zoukankan      html  css  js  c++  java
  • Java解压和压缩带密码的zip文件

    提示:本文介绍的是winzipaes项目,但该开源项目使用起来并不太方便,我最终也没有采用它,如果您有在Java语言环境中处理zip压缩文件的需要,推荐采用zip4j这一开源项目,相比winzipaes仅支持AES算法而言,zip4j支持多种算法,其它方面也是非常优秀,可以说是强大。

    详见另一篇博客:


    前言


    JDK自带的ZIP操作接口(java.util.zip包,请参看文章末尾的博客链接)并不支持密码,甚至也不支持中文文件名。

    为了解决ZIP压缩文件的密码问题,在网上搜索良久,终于找到了winzipaes开源项目。

    该项目在google code下托管,网址为:http://code.google.com/p/winzipaes ,仅支持AES压缩和解压zip文件( This library only supports Win-Zip's 256-Bit AES mode.)。网站上下载的文件是源代码,最新版本为winzipaes_src_20120416.zip,本示例就是在此基础上编写。


    详述


    项目使用很简单,利用源码自己导出一个jar文件,在项目中引用即可。

    这里有一个需要注意的问题,就是如果给定ZIP文件没有密码,那么就不能使用该项目解压,如果压缩文件没有密码却使用该项目解压在这里会报一个异常,所以使用中需要注意:加密ZIP文件可以使用它解压,没有加密的就需要采取其它方式了。

    另外:直接从Google Code上取下来的项目是不支持含有中文的的文件名的,因为winzipaes默认采用的是“ISO-8859-1”编码,但是网上有人稍微修改了一下源码,使其支持中文文件名,请参照这里:http://xjlsgcjdtc.iteye.com/blog/1439514

    此文就是采用修改后的winzipaes编写,并记录详细修改步骤。

    winzipaes项目依赖bcprov的jar包,可以到 http://www.bouncycastle.org/java.html 上下载。


    示例


    在研究该项目时写了一个工具类,本来准备用在项目中,最后找到了更好的解决方案zip4j来代替,所以最终没有采用。

    [java] view plaincopy
    1. package com.ninemax.demo.zip.decrypt;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.util.List;  
    6. import java.util.zip.DataFormatException;  
    7.   
    8. import org.apache.commons.io.FileUtils;  
    9.   
    10. import de.idyl.winzipaes.AesZipFileDecrypter;  
    11. import de.idyl.winzipaes.AesZipFileEncrypter;  
    12. import de.idyl.winzipaes.impl.AESDecrypter;  
    13. import de.idyl.winzipaes.impl.AESDecrypterBC;  
    14. import de.idyl.winzipaes.impl.AESEncrypter;  
    15. import de.idyl.winzipaes.impl.AESEncrypterBC;  
    16. import de.idyl.winzipaes.impl.ExtZipEntry;  
    17.   
    18. /** 
    19.  * 压缩指定文件或目录为ZIP格式压缩文件 
    20.  * 支持中文(修改源码后) 
    21.  * 支持密码(仅支持256bit的AES加密解密) 
    22.  * 依赖bcprov项目(bcprov-jdk16-140.jar) 
    23.  *  
    24.  * @author zyh 
    25.  */  
    26. public class DecryptionZipUtil {  
    27.       
    28.     /** 
    29.      * 使用指定密码将给定文件或文件夹压缩成指定的输出ZIP文件 
    30.      * @param srcFile 需要压缩的文件或文件夹 
    31.      * @param destPath 输出路径 
    32.      * @param passwd 压缩文件使用的密码 
    33.      */  
    34.     public static void zip(String srcFile,String destPath,String passwd) {  
    35.         AESEncrypter encrypter = new AESEncrypterBC();  
    36.         AesZipFileEncrypter zipFileEncrypter = null;  
    37.         try {  
    38.             zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);  
    39.             /** 
    40.              * 此方法是修改源码后添加,用以支持中文文件名 
    41.              */  
    42.             zipFileEncrypter.setEncoding("utf8");  
    43.             File sFile = new File(srcFile);  
    44.             /** 
    45.              * AesZipFileEncrypter提供了重载的添加Entry的方法,其中: 
    46.              * add(File f, String passwd)  
    47.              *          方法是将文件直接添加进压缩文件 
    48.              *  
    49.              * add(File f,  String pathForEntry, String passwd) 
    50.              *          方法是按指定路径将文件添加进压缩文件 
    51.              * pathForEntry - to be used for addition of the file (path within zip file) 
    52.              */  
    53.             doZip(sFile, zipFileEncrypter, "", passwd);  
    54.         } catch (IOException e) {  
    55.             e.printStackTrace();  
    56.         } finally {  
    57.             try {  
    58.                 zipFileEncrypter.close();  
    59.             } catch (IOException e) {  
    60.                 e.printStackTrace();  
    61.             }  
    62.         }  
    63.     }  
    64.       
    65.     /** 
    66.      * 具体压缩方法,将给定文件添加进压缩文件中,并处理压缩文件中的路径 
    67.      * @param file 给定磁盘文件(是文件直接添加,是目录递归调用添加) 
    68.      * @param encrypter AesZipFileEncrypter实例,用于输出加密ZIP文件 
    69.      * @param pathForEntry ZIP文件中的路径 
    70.      * @param passwd 压缩密码 
    71.      * @throws IOException 
    72.      */  
    73.     private static void doZip(File file, AesZipFileEncrypter encrypter,  
    74.             String pathForEntry, String passwd) throws IOException {  
    75.         if (file.isFile()) {  
    76.             pathForEntry += file.getName();  
    77.             encrypter.add(file, pathForEntry, passwd);  
    78.             return;  
    79.         }  
    80.         pathForEntry += file.getName() + File.separator;  
    81.         for(File subFile : file.listFiles()) {  
    82.             doZip(subFile, encrypter, pathForEntry, passwd);  
    83.         }  
    84.     }  
    85.       
    86.     /** 
    87.      * 使用给定密码解压指定压缩文件到指定目录 
    88.      * @param inFile 指定Zip文件 
    89.      * @param outDir 解压目录 
    90.      * @param passwd 解压密码 
    91.      */  
    92.     public static void unzip(String inFile, String outDir, String passwd) {  
    93.         File outDirectory = new File(outDir);  
    94.         if (!outDirectory.exists()) {  
    95.             outDirectory.mkdir();  
    96.         }  
    97.         AESDecrypter decrypter = new AESDecrypterBC();  
    98.         AesZipFileDecrypter zipDecrypter = null;  
    99.         try {  
    100.             zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);  
    101.             AesZipFileDecrypter.charset = "utf-8";  
    102.             /** 
    103.              * 得到ZIP文件中所有Entry,但此处好像与JDK里不同,目录不视为Entry 
    104.              * 需要创建文件夹,entry.isDirectory()方法同样不适用,不知道是不是自己使用错误 
    105.              * 处理文件夹问题处理可能不太好 
    106.              */  
    107.             List<ExtZipEntry> entryList = zipDecrypter.getEntryList();  
    108.             for(ExtZipEntry entry : entryList) {  
    109.                 String eName = entry.getName();  
    110.                 String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);  
    111.                 File extractDir = new File(outDir, dir);  
    112.                 if (!extractDir.exists()) {  
    113.                     FileUtils.forceMkdir(extractDir);  
    114.                 }  
    115.                 /** 
    116.                  * 抽出文件 
    117.                  */  
    118.                 File extractFile = new File(outDir + File.separator + eName);  
    119.                 zipDecrypter.extractEntry(entry, extractFile, passwd);  
    120.             }  
    121.         } catch (IOException e) {  
    122.             e.printStackTrace();  
    123.         } catch (DataFormatException e) {  
    124.             e.printStackTrace();  
    125.         } finally {  
    126.             try {  
    127.                 zipDecrypter.close();  
    128.             } catch (IOException e) {  
    129.                 e.printStackTrace();  
    130.             }  
    131.         }  
    132.     }  
    133.   
    134.     /** 
    135.      * 测试 
    136.      * @param args 
    137.      */  
    138.     public static void main(String[] args) {  
    139.         /** 
    140.          * 压缩测试 
    141.          * 可以传文件或者目录 
    142.          */  
    143. //      zip("M:\ZIP\test\bb\a\t.txt", "M:\ZIP\test\temp1.zip", "zyh");  
    144. //      zip("M:\ZIP\test\bb", "M:\ZIP\test\temp2.zip", "zyh");  
    145.           
    146.         unzip("M:\ZIP\test\temp2.zip""M:\ZIP\test\temp""zyh");  
    147.     }  
    148. }  

    压缩多个文件时,有两个方法(第一种没试):

    (1) 预先把多个文件压缩成zip,然后调用enc.addAll(inZipFile, password);方法将多个zip文件加进来。

    (2)针对需要压缩的文件循环调用enc.add(inFile, password);,每次都用相同的密码。


    修改源码后的项目可到上面提到的博客去下载,或者参照博客自己修改,其实也很容易,毕竟只有几处改动。

    另外我的CSDN下载频道也上传了修改后的源码和jar包,也可以去那里下载。



    修改记录


    参考http://xjlsgcjdtc.iteye.com/blog/1439514

    需要修改的文件有:
    • ExtZipOutputStream
    • ExtZipEntry
    • AesZipFileEncrypter

    在ExtZipOutputStream里增加一成员变量并添加两个方法:

    [java] view plaincopy
    1. protected String encoding = "iso-8859-1";     
    2. public boolean utf8Flg = false;  

    [java] view plaincopy
    1. public void setEncoding(String encoding) {  
    2.     this.encoding = encoding;  
    3.     utf8Flg |= isUTF8(encoding);  
    4. }  
    [java] view plaincopy
    1. protected boolean isUTF8(String encoding) {  
    2.        if (encoding == null) {  
    3.            // check platform's default encoding  
    4.            encoding = System.getProperty("file.encoding");  
    5.        }  
    6.        return "UTF8".equalsIgnoreCase(encoding)  
    7.            || "UTF-8".equalsIgnoreCase(encoding);  
    8.    }  

    然后将ExtZipOutputStream的(134行和158行左右)iso-8859-1编码替换成上面设置的编码格式 
    接着,再将106行左右文件名长度取得代码改成

    [java] view plaincopy
    1. writeShort(entry.getName().getBytes(encoding).length); // file name length  

    这里有个地方需要注意,当文件名是utf8编码格式的时候,需要设置Zip包的通用位标志 (不明白)
    第十一个比特为1,代码修改如下: 
    修改ExtZipEntry类在initEncryptedEntry方法基础上增加一个重载方法:

    [java] view plaincopy
    1. public void initEncryptedEntry(boolean utf8Flag) {  
    2.     setCrc(0); // CRC-32 / for encrypted files it's 0 as AES/MAC checks integritiy  
    3.   
    4.     this.flag |= 1// bit0 - encrypted  
    5.     if (utf8Flag) {  
    6.         this.flag |=(1 << 11);  
    7.     }  
    8.     // flag |= 8; // bit3 - use data descriptor  
    9.     this.primaryCompressionMethod = 0x63;  
    10.   
    11.     byte[] extraBytes = new byte[11];  
    12.     extraBytes = new byte[11];  
    13.   
    14.     // extra data header ID for AES encryption is 0x9901  
    15.     extraBytes[0] = 0x01;  
    16.     extraBytes[1] = (byte)0x99;  
    17.   
    18.     // data size (currently 7, but subject to possible increase in the  
    19.     // future)  
    20.     extraBytes[2] = 0x07// data size  
    21.     extraBytes[3] = 0x00// data size  
    22.     // Integer version number specific to the zip vendor  
    23.     extraBytes[4] = 0x02// version number  
    24.     extraBytes[5] = 0x00// version number  
    25.   
    26.     // 2-character vendor ID  
    27.     extraBytes[6] = 0x41// vendor id  
    28.     extraBytes[7] = 0x45// vendor id  
    29.   
    30.     // AES encryption strength - 1=128, 2=192, 3=256  
    31.     extraBytes[8] = 0x03;  
    32.   
    33.     // actual compression method - 0x0000==stored (no compression) - 2 bytes  
    34.     extraBytes[9] = (byte) (getMethod() & 0xff);  
    35.     extraBytes[10] = (byte) ((getMethod() & 0xff00) >> 8);  
    36.   
    37.     setExtra(extraBytes);  
    38. }  

    其实就是增加一个参数并增加了下面这段代码:

    [java] view plaincopy
    1. if (utf8Flag) {  
    2.     this.flag |=(1 << 11);  
    3. }  
    当然不要忘了将调用该方法地方修改一下,传进utf8Flag参数

    AesZipFileEncrypter类里有两处(在两个add方法中)其它地方不需改动。


    注:以上代码我自己已测试通过,如果哪位朋友测试出错,请留言!


    Java基本JDK压缩和解压ZIP文件请参照:

    http://blog.csdn.net/zhangyihui1986/article/details/7723649


    相关文件下载

    http://download.csdn.net/detail/zhangyihui1986/4415937


  • 相关阅读:
    BootStrap弹出框插件popover简单实例
    peity(jQuery 插件可以将元素内容转换为一个小的 <svg> 饼图,圆环图,条形图和折线图)
    java生成API文档
    搭建eova开发环境
    搭建jfinal+maven框架
    注解Annotation
    【转】hive时间函数
    [hive][转]hive修改 表/分区语句
    【java findbugs集锦】【转】May expose internal representation by incorporating reference to mutable object
    [java学习笔记]继承和组合
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318268.html
Copyright © 2011-2022 走看看