Apache自带包打包文件夹成zip包

  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.util.HashMap;   
  6. import java.util.Map;   
  7. import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;   
  8. import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;   
  9. import org.apache.commons.logging.Log;   
  10. import org.apache.commons.logging.LogFactory;   
  11.   
  12. /**  
  13.  *  
  14.  * @author teddy 
  15.  */  
  16. public final class ZipUtil {   
  17.   
  18.     private static final Log log = LogFactory.getLog(ZipUtil.class);   
  19.   
  20.     /**  
  21.      * 打包文件  
  22.      * @param files 文件或文件夹的集合  
  23.      * @param out 输出的zip文件  
  24.      */  
  25.     public static void zip(File[] files, File out) {   
  26.         if (files != null) {   
  27.             Map<String, File> map = new HashMap<String, File>();   
  28.             for (File f : files) {   
  29.                 list(f, null, map);   
  30.             }   
  31.             if (!map.isEmpty()) {   
  32.                 try {   
  33.                     ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(out);   
  34.                     for (Map.Entry<String, File> entry : map.entrySet()) {   
  35.                         File file = entry.getValue();   
  36.                         ZipArchiveEntry zae = new ZipArchiveEntry(file, entry.getKey());   
  37.                         zaos.putArchiveEntry(zae);   
  38.                         InputStream is = new FileInputStream(file);   
  39.                         byte[] b = new byte[1024 * 5];   
  40.                         int i = -1;   
  41.                         while ((i = is.read(b)) != -1) {   
  42.                             zaos.write(b, 0, i);   
  43.                         }   
  44.                         is.close();   
  45.                         zaos.closeArchiveEntry();   
  46.                     }   
  47.                     zaos.finish();   
  48.                     zaos.close();   
  49.                 } catch (IOException ex) {   
  50.                     log.error(ex.getMessage(), ex);   
  51.                 }   
  52.             }   
  53.         }   
  54.     }   
  55.   
  56.     private static void list(File f, String parent, Map<String, File> map) {   
  57.         String name = f.getName();   
  58.         if (parent != null) {   
  59.             name = parent + "/" + name;//设置在zip包里的相对路径   
  60.         }   
  61.         if (f.isFile()) {   
  62.             map.put(name, f);   
  63.         } else if (f.isDirectory()) {   
  64.             for (File file : f.listFiles()) {   
  65.                 list(file, name, map);   
  66.             }   
  67.         }   
  68.     }   
  69. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值