zip压缩解决文件名中文乱码问题(2)

使用apache旗下的commons-compress 压缩和解压zip文件

可以参考我的前一篇博客:http://hw1287789687.iteye.com/blog/1976309

以下是我封装的一个工具类,专门负责zip的压缩和解压 

CompressZipUtil:

Java代码   收藏代码
  1. package com.common.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import org.apache.commons.compress.archivers.ArchiveException;  
  11. import org.apache.commons.compress.archivers.ArchiveInputStream;  
  12. import org.apache.commons.compress.archivers.ArchiveOutputStream;  
  13. import org.apache.commons.compress.archivers.ArchiveStreamFactory;  
  14. import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;  
  15. import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;  
  16. import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;  
  17.   
  18. import com.io.hw.file.util.FileUtils;  
  19. import com.string.widget.util.ValueWidget;  
  20. import com.swing.messagebox.GUIUtil23;  
  21.   
  22. /*** 
  23.  * use apache 
  24.  * commons-compress,http://www.cnblogs.com/un4sure/archive/2011/09/27/ 
  25.  * 2193298.html,http://hw1287789687.iteye.com/blog/1976309 
  26.  *  
  27.  * @author huangwei 
  28.  * @since 2013-11-18 
  29.  */  
  30. public final class CompressZipUtil {  
  31.     private CompressZipUtil() {  
  32.         throw new Error("Don't let anyone instantiate this class.");  
  33.     }  
  34.   
  35.     /*** 
  36.      *  
  37.      * @param zipOut 
  38.      * @param filepath 
  39.      *            :要压缩的单个文件 
  40.      * @throws IOException 
  41.      */  
  42.     public static void addEntry(ZipArchiveOutputStream zipOut, String filepath)  
  43.             throws IOException {  
  44.         File singleFile = new File(filepath);  
  45.         if (singleFile.isDirectory()) {  
  46.             return;  
  47.         }  
  48.         ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(singleFile,  
  49.                 SystemUtil.getFileSimpleName(filepath));  
  50.         zipOut.putArchiveEntry(zipEntry2);  
  51.         FileInputStream fin = new FileInputStream(filepath);  
  52.         // 不要关闭zipOut,关闭之前要执行closeArchiveEntry()  
  53.         FileUtils.writeIn2Output(fin, zipOut, falsetrue);  
  54.     }  
  55.   
  56.     /*** 
  57.      *  
  58.      * @param zipOut 
  59.      * @param filepath 
  60.      *            : 要压缩的单个文件 
  61.      * @throws IOException 
  62.      */  
  63.     public static void addEntry(ZipArchiveOutputStream zipOut, File filepath)  
  64.             throws IOException {  
  65.         if (filepath.isDirectory()) {  
  66.             return;  
  67.         }  
  68.         ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(filepath,  
  69.                 filepath.getName());  
  70.         zipOut.putArchiveEntry(zipEntry2);  
  71.         FileInputStream fin = new FileInputStream(filepath);  
  72.         // 不要关闭zipOut,关闭之前要执行closeArchiveEntry()  
  73.         FileUtils.writeIn2Output(fin, zipOut, falsetrue);  
  74.     }  
  75.   
  76.     /*** 
  77.      *  
  78.      * @param zipOut 
  79.      * @param filePaths 
  80.      *            :要压缩的文件 
  81.      * @throws IOException 
  82.      */  
  83.     public static void compressZip(ZipArchiveOutputStream zipOut,  
  84.             @SuppressWarnings("rawtypes") List filePaths) throws IOException {  
  85.   
  86.         for (Object fileObj : filePaths) {  
  87.             if (fileObj instanceof File) {  
  88.                 File file = (File) fileObj;  
  89.                 addEntry(zipOut, file);  
  90.             } else {  
  91.                 addEntry(zipOut, (String) fileObj);  
  92.             }  
  93.         }  
  94.         zipOut.closeArchiveEntry();  
  95.         zipOut.flush();  
  96.         zipOut.finish();  
  97.         zipOut.close();  
  98.     }  
  99.   
  100.     /*** 
  101.      * 压缩. 
  102.      *  
  103.      * @param zipFile 
  104.      *            :压缩的结果--zip文件 
  105.      * @param filePaths 
  106.      * @throws IOException 
  107.      * @throws ArchiveException 
  108.      */  
  109.     public static void compressZip(File zipFile,  
  110.             @SuppressWarnings("rawtypes") List filePaths) throws IOException,  
  111.             ArchiveException {  
  112.         FileOutputStream fou = new FileOutputStream(zipFile);  
  113.         ArchiveOutputStream archOuts = new ArchiveStreamFactory()  
  114.                 .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);  
  115.         if (archOuts instanceof ZipArchiveOutputStream) {  
  116.             ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;  
  117.             compressZip(zipOut, filePaths);  
  118.         }  
  119.     }  
  120.   
  121.     /*** 
  122.      *  
  123.      * @param zipFile 
  124.      *            :压缩的结果--zip文件 
  125.      * @param filePaths 
  126.      * @throws IOException 
  127.      * @throws ArchiveException 
  128.      */  
  129.     public static void compressZip(String zipFile,  
  130.             @SuppressWarnings("rawtypes") List filePaths) throws IOException,  
  131.             ArchiveException {  
  132.         compressZip(new File(zipFile), filePaths);  
  133.     }  
  134.   
  135.     /*** 
  136.      * compress single file. 
  137.      *  
  138.      * @param zipFile 
  139.      * @param filepath 
  140.      *            :single file 
  141.      * @throws IOException 
  142.      * @throws ArchiveException 
  143.      */  
  144.     public static void compressSingeFile(String zipFile, String filepath)  
  145.             throws IOException, ArchiveException {  
  146.         List filePaths = new ArrayList();  
  147.         filePaths.add(filepath);  
  148.         compressZip(zipFile, filePaths);  
  149.     }  
  150.   
  151.     /*** 
  152.      * compress single file. 
  153.      *  
  154.      * @param zipFile 
  155.      * @param filepath 
  156.      *            :single file 
  157.      * @throws IOException 
  158.      * @throws ArchiveException 
  159.      */  
  160.     public static void compressSingeFile(String zipFile, File filepath)  
  161.             throws IOException, ArchiveException {  
  162.         List filePaths = new ArrayList();  
  163.         filePaths.add(filepath);  
  164.         compressZip(zipFile, filePaths);  
  165.     }  
  166.   
  167.     /*** 
  168.      * 压缩指定文件夹中的所有文件. 
  169.      *  
  170.      * @param zipFile 
  171.      * @param folderPaths 
  172.      * @throws IOException 
  173.      * @throws ArchiveException 
  174.      */  
  175.     public static void compressZip(String zipFile, String folderPaths)  
  176.             throws IOException, ArchiveException {  
  177.         List list = FileUtils.getListFiles(new File(folderPaths));  
  178.         compressZip(zipFile, list);  
  179.     }  
  180.   
  181.     /*** 
  182.      * 压缩指定文件夹中的所有文件. 
  183.      *  
  184.      * @param zipFile 
  185.      * @param folderPaths 
  186.      * @throws IOException 
  187.      * @throws ArchiveException 
  188.      */  
  189.     public static void compressZip(String zipFile, File folderPaths)  
  190.             throws IOException, ArchiveException {  
  191.         List list = FileUtils.getListFiles(folderPaths);  
  192.         compressZip(zipFile, list);  
  193.     }  
  194.   
  195.     /*** 
  196.      * 解压zip 
  197.      *  
  198.      * @param zipFile 
  199.      * @param decompressLoc 
  200.      *            :解压之后的文件所在目录 
  201.      * @throws ArchiveException 
  202.      * @throws IOException 
  203.      */  
  204.     public static boolean decompress(String zipFile, File decompressLoc)  
  205.             throws ArchiveException, IOException {  
  206.         FileInputStream fin = new FileInputStream(zipFile);  
  207.         ArchiveInputStream archOuts = new ArchiveStreamFactory()  
  208.                 .createArchiveInputStream(ArchiveStreamFactory.ZIP, fin);  
  209.         ZipArchiveInputStream zipIn = (ZipArchiveInputStream) archOuts;  
  210.         ZipArchiveEntry zipEntry;  
  211.         while (!ValueWidget.isNullOrEmpty(zipEntry = zipIn.getNextZipEntry())) {  
  212.             String fileName = zipEntry.getName();  
  213.             File singFile = new File(decompressLoc, fileName);  
  214.             if (singFile.exists()) {//若解压后的文件已经存在,则直接退出  
  215.                 GUIUtil23.warningDialog("File \"" + singFile.getAbsolutePath()  
  216.                         + "\" does  exist.");  
  217.                 zipIn.close();  
  218. //              break;  
  219.                 return false;  
  220.             }  
  221.             System.out.println(fileName);  
  222.             FileUtils.writeIn2Output(zipIn, new FileOutputStream(singFile),  
  223.                     truefalse);  
  224.         }  
  225.         zipIn.close();  
  226.         return true;  
  227.     }  
  228.   
  229.     /*** 
  230.      * 解压zip 
  231.      *  
  232.      * @param zipFile 
  233.      * @param decompressLoc 
  234.      *            :解压之后的文件所在目录 
  235.      * @throws ArchiveException 
  236.      * @throws IOException 
  237.      */  
  238.     public static boolean decompress(String zipFile, String decompressLocStr)  
  239.             throws ArchiveException, IOException {  
  240.         File decompressLoc = new File(decompressLocStr);  
  241.         return decompress(zipFile, decompressLoc);  
  242.     }  
  243.   
  244. }  

 附件:

(1)依赖的jar包 io0007-find_progess(包含源码)

(2)例子:目前只能压缩和解压一级目录的zip文件,zip_mgmt-0.0.1-SNAPSHOT.jar

执行方式 Java -jar zip_mgmt-0.0.1-SNAPSHOT.jar:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值