使用org.apache.tools.zip实现zip压缩和解压

  1 import java.io.*;
  2 import org.apache.tools.zip.*;
  3 import java.util.Enumeration;
  4 /**
  5 *功能:zip压缩、解压(支持中文文件名)
  6 *说明:本程序通过使用Apache Ant里提供的zip工具org.apache.tools.zip实现了zip压缩和解压功能.
  7 *   解决了由于java.util.zip包不支持汉字的问题。
  8 *   使用java.util.zip包时,当zip文件中有名字为中文的文件时,
  9 *   就会出现异常:"Exception  in thread "main " java.lang.IllegalArgumentException  
 10 *               at   java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
 11 *注意:
 12 *   1、使用时把ant.jar放到classpath中,程序中使用import org.apache.tools.zip.*;
 13 *   2、Apache Ant 下载地址:[url]http://ant.apache.org/[/url]
 14 *   3、Ant ZIP API:[url]http://www.jajakarta.org/ant/ant-1.6.1/docs/mix/manual/api/org/apache/tools/zip/[/url]
 15 *   4、本程序使用Ant 1.7.1 中的ant.jar
 16 *
 17 *仅供编程学习参考.
 18 *
 19 *@author Winty
 20 *@date   2008-8-3
 21 *@Usage:
 22 *   压缩:java AntZip -zip "directoryName"
 23 *   解压:java AntZip -unzip "fileName.zip"
 24 */
 25 
 26 public class AntZip{
 27     private ZipFile         zipFile;
 28     private ZipOutputStream zipOut;     //压缩Zip
 29     private ZipEntry        zipEntry;
 30     private static int      bufSize;    //size of bytes
 31     private byte[]          buf;
 32     private int             readedBytes;
 33     
 34     public AntZip(){
 35         this(512);
 36     }
 37 
 38     public AntZip(int bufSize){
 39         this.bufSize = bufSize;
 40         this.buf = new byte[this.bufSize];
 41     }
 42     
 43     //压缩文件夹内的文件
 44     public void doZip(String zipDirectory){//zipDirectoryPath:需要压缩的文件夹名
 45         File file;
 46         File zipDir;
 47 
 48         zipDir = new File(zipDirectory);
 49         String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名
 50 
 51         try{
 52             this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
 53             handleDir(zipDir , this.zipOut);
 54             this.zipOut.close();
 55         }catch(IOException ioe){
 56             ioe.printStackTrace();
 57         }
 58     }
 59 
 60     //由doZip调用,递归完成目录文件读取
 61     private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
 62         FileInputStream fileIn;
 63         File[] files;
 64 
 65         files = dir.listFiles();
 66     
 67         if(files.length == 0){//如果目录为空,则单独创建之.
 68             //ZipEntry的isDirectory()方法中,目录以"/"结尾.
 69             this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
 70             this.zipOut.closeEntry();
 71         }
 72         else{//如果目录不为空,则分别处理目录和文件.
 73             for(File fileName : files){
 74                 //System.out.println(fileName);
 75 
 76                 if(fileName.isDirectory()){
 77                     handleDir(fileName , this.zipOut);
 78                 }
 79                 else{
 80                     fileIn = new FileInputStream(fileName);
 81                     this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
 82 
 83                     while((this.readedBytes = fileIn.read(this.buf))>0){
 84                         this.zipOut.write(this.buf , 0 , this.readedBytes);
 85                     }
 86 
 87                     this.zipOut.closeEntry();
 88                 }
 89             }
 90         }
 91     }
 92 
 93     //解压指定zip文件
 94     public void unZip(String unZipfileName){//unZipfileName需要解压的zip文件名
 95         FileOutputStream fileOut;
 96         File file;
 97         InputStream inputStream;
 98 
 99         try{
100             this.zipFile = new ZipFile(unZipfileName);
101 
102             for(Enumeration entries = this.zipFile.getEntries(); entries.hasMoreElements();){
103                 ZipEntry entry = (ZipEntry)entries.nextElement();
104                 file = new File(entry.getName());
105 
106                 if(entry.isDirectory()){
107                     file.mkdirs();
108                 }
109                 else{
110                     //如果指定文件的目录不存在,则创建之.
111                     File parent = file.getParentFile();
112                     if(!parent.exists()){
113                         parent.mkdirs();
114                     }
115 
116                     inputStream = zipFile.getInputStream(entry);
117 
118                     fileOut = new FileOutputStream(file);
119                     while(( this.readedBytes = inputStream.read(this.buf) ) > 0){
120                         fileOut.write(this.buf , 0 , this.readedBytes );
121                     }
122                     fileOut.close();
123 
124                     inputStream.close();
125                 }    
126             }
127             this.zipFile.close();
128         }catch(IOException ioe){
129             ioe.printStackTrace();
130         }
131     }
132 
133     //设置缓冲区大小
134     public void setBufSize(int bufSize){
135         this.bufSize = bufSize;
136     }
137 
138     //测试AntZip类
139     public static void main(String[] args)throws Exception{
140         if(args.length==2){
141             String name = args[1];
142             AntZip zip = new AntZip();
143 
144             if(args[0].equals("-zip"))
145                 zip.doZip(name);
146             else if(args[0].equals("-unzip"))
147                 zip.unZip(name);
148         }
149         else{
150             System.out.println("Usage:");
151             System.out.println("压缩:java AntZip -zip directoryName");
152             System.out.println("解压:java AntZip -unzip fileName.zip");
153             throw new Exception("Arguments error!");
154         }
155     }
156 }

版权所有,转载请务必保留此出处http://wintys.blog.51cto.com/425414/90878

posted @ 2012-04-30 13:00  wangrs  阅读(5691)  评论(0编辑  收藏  举报