agou

导航

让ZipOutputStream和ZipInputStream支持中文

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)); 

posted on 2005-11-09 17:44  阿狗  阅读(802)  评论(0)    收藏  举报