`

java zip 中文问题_基于Thinking in java 4th io.ZipCompress_文件名和文件内容

阅读更多
说明:
1、文件内容采用XXXReader、XXXWriter面向字符的处理类
2、由于jdk1.6的ZipOutputStream没有方法setEncoding(encoding),故采用ant.jar的ZipOutputStream.
  ant包,可以在官方网站http://ant.apache.org/bindownload.cgi下载,把ant.jar导入到类中,
我下载的是apache-ant-1.8.2-bin.zip,解压后得到相关jar包 和 关联文档(读文档很必要的哦)

具体见代码:
代码不规范、有点乱,请见谅

package io;
//: io/ZipCompress2.java
// Uses Zip compression to compress any
// number of files given on the command line.
// {Args: ZipCompress2.java}

//import java.util.zip.*;
import org.apache.tools.zip.*; //不使用jdk的rt.jar中的类,转而使用ant.jar中的类。  Apache Ant is a Java library and command-line tool that help building software

import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Adler32;
import java.util.zip.ZipInputStream;
import java.io.*;
import java.util.*;
import static net.mindview.util.Print.*;

//ant包,可以在官方网站http://ant.apache.org/bindownload.cgi下载,把ant.jar导入到类中
public class ZipCompress2 {
  public static void main(String[] args)
  throws IOException {
 
  //(1)使用org.apache.tools.zip.ZipOutputStream 写压缩包test测试_ant.zip到文件系统
  FileOutputStream f = new FileOutputStream("test测试_ant.zip");
    CheckedOutputStream csum =
      new CheckedOutputStream(f, new Adler32());
     ZipOutputStream zos = new ZipOutputStream(csum); //
     String encoding=System.getProperty("file.encoding");
     zos.setEncoding(encoding);
    
     //BufferedOutputStream out =
      //new BufferedOutputStream(zos);
    PrintWriter out = new PrintWriter(zos);
    zos.setComment("A test of Java Zipping中文注释"); //???怎么解决中文注解显示为乱码的情况????? 采用ant.jar中的压缩相关类
    // No corresponding getComment(), though.
   
    for(String arg : args) {
      print("Writing file " + arg);
      BufferedReader in =new BufferedReader(new FileReader(arg));
      zos.putNextEntry(new ZipEntry(arg));//加入归档包/压缩包的每一个文件,都对应一个ZipEntry。  ???怎么解决文件名为中文显示为乱码的情况????? 采用ant.jar中的压缩相关类
      /* 1.For each file to add to the archive, you must call putNextEntry( ) and pass it a ZipEntry object.
       *
       * The ZipEntry object contains an extensive interface that allows you to get and set all the data
       * available on that particular entry in your Zip file: name, compressed and uncompressed sizes,
       *date, CRC checksum, extra field data, comment, compression method, and whether it’s a directory entry */
      int c;
      while((c = in.read()) != -1)
        out.write(c);//2.BufferedOutputStream out = new BufferedOutputStream(zos);
      /*通过out将数据输出/写入到zos,但是每一个文件对应一个in=new BufferedReader(new FileReader(arg)),
       *每一个文件对应一个ZipEntry:zos.putNextEntry(new ZipEntry(arg))*/
      in.close();
      out.flush();
    }
    out.close();
   
        //(2)
    /*由于在java.util.zip.ZipOutputStream中对ZipEntry.name采用的是utf-8编码: byte[] nameBytes = getUTF8Bytes(e.name)
     *    java.util.zip.ZipInputStream中也是对ZipEntry.name采用的是utf-8编码:ZipEntry e = createZipEntry(getUTF8String(b, 0, len))
       所以在读取zip包中的文件时,可以兼容(都是用utf-8编码文件名)
      
       但是在ant.jar的org.apache.tools.zip.* 中没有ZipInputStream类,这是就只能使用org.apache.tools.zip.ZipFile了.
      
      当使用java.util.zip.ZipInputStream时报错:
       at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:303)
    这是因为org.apache.tools.zip.ZipOutputStream使用GB18030编码文件名,却在java.util.zip.ZipInputStream中使用utf-8解码文件名
     */
   
    /*
    // Checksum valid only after the file has been closed!
    print("Checksum: " + csum.getChecksum().getValue()); //In order to read the checksum, you must somehow have access to the associated Checksum(表示数据校验和的接口) object
    // Now extract the files:
    print("Reading file");
    FileInputStream fi = new FileInputStream("test测试_ant.zip");
    CheckedInputStream csumi =
      new CheckedInputStream(fi, new Adler32());
    ZipInputStream zis = new ZipInputStream(csumi);
    //BufferedInputStream bis = new BufferedInputStream(zis); 
    BufferedReader bis=new BufferedReader( new InputStreamReader(zis));
    java.util.zip.ZipEntry ze;
   
    while((ze = zis.getNextEntry()) != null) {
      print("Reading file " + ze);
      int x;
      while((x = bis.read()) != -1)
        System.out.print((char)x); //System.out.write()是面向字节的
      print();
    }
    if(args.length == 1)
       print("Checksum: " + csumi.getChecksum().getValue());
    bis.close();
    print();
    */
   
    //(3)采用org.apache.tools.zip.ZipFile读取zip文件
    org.apache.tools.zip.ZipFile zf=new org.apache.tools.zip.ZipFile("test测试_ant.zip",encoding);
    Enumeration<org.apache.tools.zip.ZipEntry>  e_zEntries=zf.getEntries();
    while(e_zEntries.hasMoreElements()){
    org.apache.tools.zip.ZipEntry ze=e_zEntries.nextElement();
    print("File: " + ze);
    BufferedReader bf=new BufferedReader( new InputStreamReader(zf.getInputStream(ze)) );
    int x;
      while((x = bf.read()) != -1)
        System.out.print((char)x); //System.out.write()是面向字节的
      print();
    }
  }
} /* (Execute to see output) *///:~
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics