我研究了一下代码,发现ZipInputStream里面默认是用getUTF8String()来实现字符的读取的,所以对于中文可能有处理起来就有问题,如果想要解决,可以按下面的方法进行.
1.重建zip包
新建一个自己的zip包,比如com.agile.zip,在这个包中把要用到的类从jdk的源码里放到这里,用eclipse可以很同快地完成这个工作。需要所类有:DeflaterOutputStream,InflaterInputStream,ZipConstants,ZipEntry,ZipInputStream,ZipOutputStream
上面这些类在放到com.aigle.zip包中后,在Eclipse中会显示出一些错误,这里要做得就是更改import以及其它一些工作,惟一的目的就是通过编译,不再出现编译错误。
2.修改ZipInputStream
ZipInputStream这个类中,需要修改getUTF8String这个方法,经过试验,用winRar压缩后的zip文件,其中的中文文件名是以gbk编码保存的,因此只需要在这个方法的前面加上
try {
String s = new String(b,off,len,"gbk");
return s;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
修改后的方法类似下面的代码:
private static String getUTF8String(byte[] b, int off, int len) {
try {
String s = new String(b,off,len,"gbk");
return s;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// First, count the number of characters in the sequence
int count = 0;
int max = off + len;
...
...
}
3.去掉一些本地调用方法
原来的java.util.zip.ZipEntry里面,要加载本地库,在这里这些代码是没有用处的,去掉就可以了,而如果不去掉,会引起链接错误,很奇怪,这几个native方法我也没有找到在哪儿使用了,sun的jdk1.4.2里留着它们做什么呢?
static {
/* load the zip library */
java.security.AccessController.doPrivileged(
new sun.security.action.LoadLibraryAction("zip"));
initIDs();
}
zip解压缩中文文件名乱码问题
最新推荐文章于 2024-07-22 09:15:28 发布