ZipInputStream不能正确处理中文文件名 (问题接踵而来)

      FD程序开发 2005-4-27 9:58
若是以ZipInputStream及ZipOutputStream来处理压缩及解压缩的工作,碰到中文档名或路径,那当然是以unicode来处理罗!
但是,现在市面上的压缩及解压缩软体,例如winzip,却是不支援unicode的,一碰到档名以unicode编码的档案,它就不处理。
那要如何才能做出让winzip能够处理的压缩档呢?那就得从修改ZipInputStream及ZipOutputStream对於档名的编码方式来着手了。我们可以从jdk的src.zip取得ZipInputStream及ZipOutputStream的原始码来加以修改:

一、ZipOutputStream.java
1.从jdk的src.zip取得ZipOutputStream.java原始码,另存新档存到c:\java\util\zip这个资料夹里,档名改为CZipOutputStream.java。
2.开始修改原始码,将class名称改为CZipOutputStream
3.建构式也必须更改为CZipOutputStream
4.新增member,这个member记录编码方式
private String encoding="UTF-8";
5.再新增一个建构式(这个建构式可以让这个class在new的时候,设定档名的编码)
public CZipOutputStream(OutputStream out,String encoding) {
super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
usesDefaultDeflater = true;
this.encoding=encoding;
}
6.找到byte[] nameBytes = getUTF8Bytes(e.name);(有二个地方),将它修改如下:
byte[] nameBytes = null;
try
{
if (this.encoding.toUpperCase().equals("UTF-8"))
nameBytes =getUTF8Bytes(e.name);
else
nameBytes= e.name.getBytes(this.encoding);
}
catch(Exception byteE)
{
nameBytes=getUTF8Bytes(e.name);
}
7.将档案储存在c:\java\util\zip这个资料夹内,请记得一定要有这个路径结构,
才能把CZipOutputStream.class放在正确的package结构里


二、ZipInputStream.java
1.从jdk的src.zip取得ZipInputStream.java原始码,另存新档存到c:\java\util\zip这个资料夹里,档名改为CZipInputStream.java。
2.开始修改原始码,将class名称改为CZipInputStream
3.建构式也必须更改为CZipInputStream
4.新增member,这个member记录编码方式
private String encoding="UTF-8";
5.再新增一个建构式如下(这个建构式可以让这个class在new的时候,设定档名的编码)
public CZipInputStream(InputStream in,String encoding) {
super(new PushbackInputStream(in,512),new Inflater(true),512);
usesDefaultInflater = true;
if(in == null) {
throw new NullPointerException("in is null");
}
this.encoding=encoding;
}

6.找到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));
}
7.将档案储存在c:\java\util\zip这个资料夹内,请记得一定要有这个路径结构,才能把CZipInputStream.class放在正确的package结构里


以上两个档案储存後compile产生CZipOutputStream.class及CZipInputStream.class,使用winzip开启[java_home]\jre\lib\rt.jar这个档案,将CZipOutputStream.class及CZipInputStream.class加进去,记得「Save full path info」一定要打勾。
以後当压缩及解压缩时有中文档名及路径的问题时,就可以指定编码方式来处理了。

CZipOutputStream zos=new CZipOutputStream(OutputStream os,String encoding);
CZipInputStream zins=new CZipInputStream(InputStream ins,String encoding);
以「压缩与解压缩(1)」为例:
FileOutputStream fos =new FileOutputStream(request.getRealPath("/")+"myzip.zip");
CZipOutputStream zos=new CZipOutputStream(fos,"GBK");
其他地方都不用改,便可以处理中文档名的压缩。
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}