当前位置: 技术问答>java相关
zip解压,中文文件名问题
来源: 互联网 发布时间:2015-11-15
本文导语: 如题。 | 今天也遇到这个问题,好不容易解决了,不过还是不爽,因为修改了相关类,而不能自己另写类。方法如下: 1.将/jdk/jre/lib/rt.jar解包 反编译ZipInputStream.class 修改getUTF8String函数为: ...
如题。
|
今天也遇到这个问题,好不容易解决了,不过还是不爽,因为修改了相关类,而不能自己另写类。方法如下:
1.将/jdk/jre/lib/rt.jar解包
反编译ZipInputStream.class
修改getUTF8String函数为:
private static String getUTF8String(byte b[], int off, int len)
{
String s = "";
try
{
s = new String(b, off, len, "GBK");
}
catch(Exception e)
{
System.out.println(e.toString());
}
return s;
}
编译,打包。
2.自己写的解包的类:
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class UnZip
{
public static void main(String filename)
{
System.out.println(filename);
File infile = new File(filename);
try{
//检查是否是zip文件
ZipFile zip = new ZipFile(infile);
zip.close();
//建立与目标文件的输入连接
ZipInputStream in = new ZipInputStream(new FileInputStream(infile));
ZipEntry file = in.getNextEntry();
int i =infile.getAbsolutePath().lastIndexOf('.');
String dirname = new String();
if ( i != -1 )
dirname = infile.getAbsolutePath().substring(0,i);
else
dirname = infile.getAbsolutePath();
File newdir = new File(dirname);
newdir.mkdir();
byte[] c = new byte[1024];
int len;
int slen;
while (file != null){
i = file.getName().replace('/','\').lastIndexOf('\');
if ( i != -1 ){
File dirs = new File(dirname+File.separator+file.getName().replace('/','\').substring(0,i));
dirs.mkdirs();
dirs = null;
}
System.out.print("extract "+file.getName().replace('/','\')+" ........ ");
if (file.isDirectory()){
File dirs = new File(file.getName().replace('/','\'));
dirs.mkdir();
dirs = null;
}
else{
FileOutputStream out = new FileOutputStream(dirname+File.separator+file.getName().replace('/','\'));
while((slen = in.read(c,0,c.length)) != -1)
out.write(c,0,slen);
out.close();
}
System.out.print("o.k.n");
file = in.getNextEntry();
}
in.close();
}catch(ZipException zipe){
System.out.println(infile.getName() + "不是一个zip文件!");
}catch(IOException ioe){
System.out.println("读取"+filename+"时错误!");
}catch(Exception i){
System.out.println("over");
}
}
}
希望能解决你的问题,大家探讨一下更好的解决办法。
1.将/jdk/jre/lib/rt.jar解包
反编译ZipInputStream.class
修改getUTF8String函数为:
private static String getUTF8String(byte b[], int off, int len)
{
String s = "";
try
{
s = new String(b, off, len, "GBK");
}
catch(Exception e)
{
System.out.println(e.toString());
}
return s;
}
编译,打包。
2.自己写的解包的类:
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class UnZip
{
public static void main(String filename)
{
System.out.println(filename);
File infile = new File(filename);
try{
//检查是否是zip文件
ZipFile zip = new ZipFile(infile);
zip.close();
//建立与目标文件的输入连接
ZipInputStream in = new ZipInputStream(new FileInputStream(infile));
ZipEntry file = in.getNextEntry();
int i =infile.getAbsolutePath().lastIndexOf('.');
String dirname = new String();
if ( i != -1 )
dirname = infile.getAbsolutePath().substring(0,i);
else
dirname = infile.getAbsolutePath();
File newdir = new File(dirname);
newdir.mkdir();
byte[] c = new byte[1024];
int len;
int slen;
while (file != null){
i = file.getName().replace('/','\').lastIndexOf('\');
if ( i != -1 ){
File dirs = new File(dirname+File.separator+file.getName().replace('/','\').substring(0,i));
dirs.mkdirs();
dirs = null;
}
System.out.print("extract "+file.getName().replace('/','\')+" ........ ");
if (file.isDirectory()){
File dirs = new File(file.getName().replace('/','\'));
dirs.mkdir();
dirs = null;
}
else{
FileOutputStream out = new FileOutputStream(dirname+File.separator+file.getName().replace('/','\'));
while((slen = in.read(c,0,c.length)) != -1)
out.write(c,0,slen);
out.close();
}
System.out.print("o.k.n");
file = in.getNextEntry();
}
in.close();
}catch(ZipException zipe){
System.out.println(infile.getName() + "不是一个zip文件!");
}catch(IOException ioe){
System.out.println("读取"+filename+"时错误!");
}catch(Exception i){
System.out.println("over");
}
}
}
希望能解决你的问题,大家探讨一下更好的解决办法。