Apache Ant包进行ZIP文件压缩

Apache Ant包进行ZIP文件压缩

许多年前就遇到过这种业务,对ZIP标准压缩文件解压。之前写的操作类现在找不到了,最近项目中又要处理这种业务,所以重新写了一个。Java提供了处理ZIP包的API。但是对中文支持不是很好,所以我直接用Apache Ant里的ZIP操作API来进行处理。ANT的API解决了中文支持问题,而且用起来也非常方便。以下是操作类。

以下的类只是用到Apache的一小部分功能。具体更多的API,请参考文档。在此不多说明了。

* 在项目中导入Apache的ant.jar包到Lib中。

 

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. }  

 

以上代码已经测试通过,支持中文目录、文件名,不限目录级别。

转载于:https://my.oschina.net/kyo153/blog/41938

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值