java zip解压缩

Java 中如何进行zip包的解压缩呢?

有两种方式:

(1)使用jdk 自带的zip工具

(2)使用apache旗下的commons-compress

我下面要讲解的zip解压缩助手使用的是apache旗下的commons-compress.

工具运行界面如下:

 核心代码:

解压:

Java代码   收藏代码
  1. /*** 
  2.      * 解压zip 
  3.      *  
  4.      * @param zipFile 
  5.      * @param decompressLoc 
  6.      *            :解压之后的文件所在目录 
  7.      * @throws ArchiveException 
  8.      * @throws IOException 
  9.      */  
  10.     public static boolean deCompressRecursion(String zipFile,  
  11.             File decompressLoc, String charSet) throws ArchiveException,  
  12.             IOException {  
  13.         FileInputStream fin = new FileInputStream(zipFile);  
  14.         ArchiveInputStream archIns = new ArchiveStreamFactory()  
  15.                 .createArchiveInputStream(ArchiveStreamFactory.ZIP, fin);  
  16.         ZipArchiveInputStream zipIn = (ZipArchiveInputStream) archIns;  
  17.         boolean isSuccess = deCompressRecursion(zipIn, decompressLoc, charSet);  
  18.         zipIn.close();  
  19.         return isSuccess;  
  20.     }  
  21. /*** 
  22.      * 递归解压缩. 
  23.      *  
  24.      * @param zipIn 
  25.      * @param decompressLoc 
  26.      * @return 
  27.      * @throws IOException 
  28.      */  
  29.     private static boolean deCompressRecursion(ZipArchiveInputStream zipIn,  
  30.             File decompressLoc, String charset) throws IOException {  
  31.         ZipArchiveEntry zipEntry;  
  32.         if (ValueWidget.isNullOrEmpty(charset)) {  
  33.             charset = SystemHWUtil.CHARSET_UTF;  
  34.         }  
  35.         while (!ValueWidget.isNullOrEmpty(zipEntry = zipIn.getNextZipEntry())) {  
  36.             byte[] rawName = zipEntry.getRawName();  
  37.             String fileName = new String(rawName, charset);  
  38.             // System.out.println(fileName);  
  39.             if (zipEntry.isDirectory()) {// 是目录  
  40.                 File newFolder = new File(decompressLoc, fileName);// 若子目录不存在,则创建之  
  41.                 System.out.println(newFolder.getAbsolutePath());  
  42.                 if (!newFolder.exists()) {  
  43.                     newFolder.mkdir();  
  44.                 }  
  45.                 // deCompressRecursion(zipIn, decompressLoc,charset);  
  46.             } else {// 是普通文件  
  47.                 File singFile = new File(decompressLoc, fileName);  
  48.                 System.out.println(singFile.getAbsolutePath());  
  49.                 if (singFile.exists()) {// 若解压后的文件已经存在,则直接退出  
  50.                     GUIUtil23.warningDialog("File \""  
  51.                             + singFile.getAbsolutePath() + "\" does  exist.");  
  52.                     return false;  
  53.                 }  
  54.                 /** 
  55.                  * 以下四行代码是后来添加的,为了解决父目录不存在的问题 
  56.                  */  
  57.                 File fatherFolder = singFile.getParentFile();  
  58.                 if (!fatherFolder.exists()) {  
  59.                     fatherFolder.mkdirs();  
  60.                 }  
  61.                 FileUtils.writeIn2Output(zipIn, new FileOutputStream(singFile),  
  62.                         truefalse);  
  63.             }  
  64.         }  
  65.         return true;  
  66.     }  

 

压缩:

Java代码   收藏代码
  1. /*** 
  2.      * 压缩文件. 
  3.      *  
  4.      * @param zipFile 
  5.      * @param folderPaths 
  6.      * @return 
  7.      * @throws ArchiveException 
  8.      * @throws IOException 
  9.      */  
  10.     public static boolean compressZipRecursion(String zipFile,  
  11.             String folderPaths) throws ArchiveException, IOException {  
  12.         FileOutputStream fou = new FileOutputStream(zipFile);  
  13.         ArchiveOutputStream archOuts = new ArchiveStreamFactory()  
  14.                 .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);  
  15.         if (archOuts instanceof ZipArchiveOutputStream) {  
  16.             ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;  
  17.             List<ZipArchiveEntry> zipEntrys = getZipFileListRecursion(new File(  
  18.                     folderPaths), null);  
  19.             for (int i = 0; i < zipEntrys.size(); i++) {  
  20.                 ZipArchiveEntry zipEntry2 = zipEntrys.get(i);  
  21.                 zipOut.putArchiveEntry(zipEntry2);  
  22.                 File file = new File(folderPaths, zipEntry2.getName());  
  23.                 if (!file.exists()) {  
  24.                     return false;  
  25.                 }  
  26.                 if (!file.isDirectory()) {  
  27.                     FileInputStream fin = new FileInputStream(file);  
  28.                     // 不要关闭zipOut,关闭之前要执行closeArchiveEntry()  
  29.                     FileUtils.writeIn2Output(fin, zipOut, falsetrue);  
  30.                 }  
  31.             }  
  32.             closeZip(zipOut, true);  
  33.   
  34.         }  
  35.         return true;  
  36.     }  
  37. /*** 
  38.      * 压缩之后的收尾操作. 
  39.      *  
  40.      * @param zipOut 
  41.      * @throws IOException 
  42.      */  
  43.     private static void closeZip(ZipArchiveOutputStream zipOut,  
  44.             boolean iscloseArchiveEntry) throws IOException {  
  45.         if (iscloseArchiveEntry) {  
  46.             zipOut.closeArchiveEntry();// it is necessary  
  47.         }  
  48.         zipOut.flush();  
  49.         zipOut.finish();  
  50.         zipOut.close();  
  51.     }  

 上述代码见类:com.common.util.CompressZipUtil

项目名:zip_mgmt

项目源代码见附件:zip_mgmt.zip

项目使用maven 构建

IDE:eclipse

依赖的jar包:(1)io0007-find_progess-0.0.8-SNAPSHOT.jar

(2)is_chinese

学习笔记见附件java zip压缩.zip

 

 

参考:http://m.blog.csdn.net/blog/buyaore_wo/7047343

http://www.cnblogs.com/un4sure/archive/2011/09/27/2193298.html,

http://hw1287789687.iteye.com/blog/1976309

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值