首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java 解压 Zip文件乱码的有关问题

2012-04-08 
java 解压 Zip文件乱码的问题我写了一段解压Zip的代码,但是如果Zip文件里面有中文文件名解压出来就是乱码。

java 解压 Zip文件乱码的问题
我写了一段解压Zip的代码,但是如果Zip文件里面有中文文件名解压出来就是乱码。
请问有没有解决的办法?

Java code
public void releaseZipToFile(String sourceZip, String outFileName)            throws IOException {        ZipFile zfile = new ZipFile(sourceZip);        Enumeration zList = zfile.entries();        ZipEntry ze = null;        byte[] buf = new byte[1024];        while (zList.hasMoreElements()) {            ze = (ZipEntry) zList.nextElement();            if (ze.isDirectory()) {                continue;            }            OutputStream os = new BufferedOutputStream(new FileOutputStream(                    getRealFileName(outFileName, ze.getName())));            InputStream is = new BufferedInputStream(zfile.getInputStream(ze));            int readLen = 0;            is.close();            os.close();                        System.out.println("Extracted: " + ze.getName());        }        zfile.close();        File file = new File(sourceZip);        file.delete();    }


[解决办法]
在java.util.zip包也可以用来处理解压问题,不过对含有中文文件名的压缩包无能为力,这是因为ZipOutputStream压缩和解压 ZIP文件对文件名都是以UTF-8编码方式来处理的,而我们用winzip压缩文件对文件名只会以ASCII编码方式来处理.所以会出现编码不一致的问题.
有两种解决方案:
第一种就是修改ZipOutputStream,参考修改如下:(这个我没有测试过)
Java code
// ZipEntry e = createZipEntry(getUTF8String(b, 0, len));ZipEntry e=null;try{    if (this.encoding.toUpperCase().equals("UTF-8"))        e=createZipEntry(getUTF8String(b, 0, len));    else        e=createZipEntry(new String(b,0,len,this.encoding));}catch(Exception byteE) {    e=createZipEntry(getUTF8String(b, 0, len));} 

热点排行