使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM