Apache Ant包進行ZIP文件壓縮

Apache Ant包進行ZIP文件壓縮javascript

許多年前就遇到過這種業務,對ZIP標準壓縮文件解壓。以前寫的操做類如今找不到了,最近項目中又要處理這種業務,因此從新寫了一個。Java提供了處理ZIP包的API。可是對中文支持不是很好,因此我直接用Apache Ant裏的ZIP操做API來進行處理。ANT的API解決了中文支持問題,並且用起來也很是方便。如下是操做類。java

如下的類只是用到Apache的一小部分功能。具體更多的API,請參考文檔。在此很少說明了。apache

* 在項目中導入Apache的ant.jar包到Lib中。app

 

Java代碼    收藏代碼
  1. /* 
  2.   * All rights reserved. 
  3.  * @author: JODY 
  4.  * @Date: 2008-05-27 
  5.  * @Time: 0:15:04 
  6.  */  
  7. package cn.com.jody.util;  
  8.   
  9. import java.io.File;  
  10. import java.io.FileOutputStream;  
  11. import java.io.InputStream;  
  12.   
  13. import org.apache.tools.zip.ZipEntry;  
  14.   
  15. /* 
  16.  * <p> 
  17.  * 功能描述 標準ZIP文件解壓縮<br> 
  18.  * 支持中文目錄、文件名<br> 
  19.  * 無限級目錄結構 
  20.  * </p> 
  21.  * 文件名稱:ExtractZIP.java<br> 
  22.  * 類型名稱:ExtractZIP<br> 
  23.  * @author: JODY 
  24.  */  
  25. public class ExtractZIP {  
  26.       
  27.     public ExtractZIP(){  
  28.           
  29.     }  
  30.     /** 
  31.      * 解壓靜態方法 
  32.      * @param zipFileName 
  33.      * @param outputDirectory 
  34.      * @throws Exception 
  35.      */  
  36.     public static void extract(String zipFileName,String outputDirectory) throws Exception{  
  37.         try {  
  38.             org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(zipFileName);  
  39.             java.util.Enumeration e = zipFile.getEntries();  
  40.   
  41.             org.apache.tools.zip.ZipEntry zipEntry = null;  
  42.   
  43.             while (e.hasMoreElements()){  
  44.                 zipEntry = (ZipEntry)e.nextElement();  
  45.                 //System.out.println("unziping "+zipEntry.getName());  
  46.                 if (zipEntry.isDirectory()){  
  47.                     String name=zipEntry.getName();  
  48.                     name=name.substring(,name.length()-1);       
  49.                     mkDirs(outputDirectory+File.separator+name);                      
  50.                     //System.out.println("建立目錄:"+outputDirectory+File.separator+name);  
  51.   
  52.                 }else{  
  53.                     String name=zipEntry.getName();  
  54.                     String dir = name.substring(,name.lastIndexOf("/"));  
  55.                     mkDirs(outputDirectory+File.separator+dir);                   
  56.                     //System.out.println("建立文件:"+outputDirectory+File.separator+name);                    
  57.                     File f=new File(outputDirectory+File.separator+zipEntry.getName());  
  58.                     f.createNewFile();  
  59.                     InputStream in = zipFile.getInputStream(zipEntry);  
  60.                     FileOutputStream out=new FileOutputStream(f);                     
  61.                     int c;  
  62.                     byte[] by=new byte[1024];  
  63.                     while((c=in.read(by)) != -1){  
  64.                         out.write(by,,c);  
  65.                     }  
  66.                     out.close();  
  67.                     in.close();  
  68.                 }  
  69.             }  
  70.         }  
  71.         catch (Exception ex){  
  72.             System.out.println("解壓文件異常"+ex.getMessage());  
  73.             ex.printStackTrace();  
  74.         }  
  75.     }  
  76.     /** 
  77.      * 建立目錄,包括子目錄 
  78.      * @param dir 
  79.      * @throws Exception 
  80.      */  
  81.     private static void mkDirs(String dir) throws Exception{  
  82.         if(dir == null || dir.equals("")) return;  
  83.         File f1 = new File(dir);  
  84.         if(!f1.exists())  
  85.             f1.mkdirs();  
  86.     }     
  87.   
  88.   
  89.     /** 
  90.      * @param args 
  91.      */  
  92.     public static void main(String[] args) {  
  93.         // TODO Auto-generated method stub  
  94.         try {  
  95.             extract("D:\\開源項目\\apache\\新建文件夾.zip""D:\\開源項目\\apache\\aa");  
  96.         } catch (Exception e) {           
  97.             e.printStackTrace();  
  98.         }  
  99.     }  
  100.   
  101. }  

 

以上代碼已經測試經過,支持中文目錄、文件名,不限目錄級別。測試

相關文章
相關標籤/搜索