Tip: This article introduces the winzipaes project, but it is not convenient to use this open-source project. I did not use it in the end, if you need to process ZIP files in a Java environment, zip4j is recommended as an open-source project. Compared with winzipaes, zip4j supports only the AES algorithm, other aspects are also very good, which can be said to be powerful.
For details, see another blog:
- Http://blog.csdn.net/zhangyihui1986/article/details/7921376
Preface
Jdk's zipoperation interface (java.util.zip package, refer to the blog link at the end of the article) does not support passwords or even Chinese file names.
To solve the password problem of zip files, I searched the internet for a long time and finally found the winzipaes open-source project.
The project is hosted under Google Code at: http://code.google.com/p/winzipaes, supporting only AES compression and decompression ZIP files (this
Library only supports win-Zip's 256-bit AES mode .). The downloaded file on the website is the source code, and the latest file is winzipaes_src_20120416.zip. This example is based on this file.
Details
The project is easy to use. Use the source code to export a jar file and reference it in the project.
Note that if a zip file does not have a password, the project cannot be decompressed. If the compressed file does not have a password but the project is decompressed, an exception is reported, therefore, you must note that you can use the encrypted ZIP file to decompress it. Otherwise, you must use other methods.
In addition: the project obtained directly from Google Code does not support the file name containing Chinese characters, because winzipaes uses the "ISO-8859-1" encoding by default, but someone on the Internet slightly modified the source code, make it support Chinese file names, refer to here: http://xjlsgcjdtc.iteye.com/blog/1439514
This article is written using the modified winzipaes and records the detailed modification steps.
The winzipaes project depends on the bcprov jar package, which can be downloaded on the http://www.bouncycastle.org/java.html.
Example
When studying this project, I wrote a tool class, which was originally intended to be used in the project. Finally, I found a better solution, zip4j, to replace it, so it was not used in the end.
Package com.ninemax.demo.zip. decrypt; import Java. io. file; import Java. io. ioexception; import Java. util. list; import java.util.zip. dataformatexception; import Org. apache. commons. io. fileutils; import de. idyl. winzipaes. aeszipfiledecrypter; import de. idyl. winzipaes. aeszipfileencrypter; import de. idyl. winzipaes. impl. aesdecrypter; import de. idyl. winzipaes. impl. aesdecrypterbc; import de. idyl. winzipaes. impl. aesencr Ypter; import de. idyl. winzipaes. impl. aesencrypterbc; import de. idyl. winzipaes. impl. extzipentry;/*** compressing a specified file or directory in ZIP format * supports Chinese characters (after the source code is modified) * supports passwords (only-bit AES encryption and decryption is supported) * dependent bcprov Project (bcprov-jdk16-140.jar) ** @ author zyh */public class decryptionziputil {/*** use the specified password to compress the specified file or folder to the specified output ZIP file * @ Param srcfile the file or folder to be compressed * @ Param destpath output path * @ Param passwd compressed file Password */public static void zip (string srcfile, str Ing destpath, string passwd) {aesencrypter encrypter = new aesencrypterbc (); encrypted zipfileencrypter = NULL; try {zipfileencrypter = new encrypted (destpath, encrypter ); /*** this method is added after the source code is modified to support Chinese file names */zipfileencrypter. setencoding ("utf8"); file sfile = new file (srcfile);/*** aeszipfileencrypter provides a method to add a reload entry, where: * Add (file F, string passwd) * The method is to directly add the file to the compressed file ** add (file F, strin G pathforentry, string passwd) * The method is to add the file to the compressed file according to the specified path * pathforentry-to be used for addition of the file (path within ZIP file) */dozip (sfile, zipfileencrypter, "", passwd);} catch (ioexception e) {e. printstacktrace ();} finally {try {zipfileencrypter. close ();} catch (ioexception e) {e. printstacktrace () ;}}/ *** specific compression method, add a given file to the compressed file, and process the path * @ Param file in the compressed file given the disk file (directly add the file, recursively call the Directory) * @ Param encry Pter aeszipfileencrypter instance, used to output the path * @ Param passwd compression password * @ throws ioexception */Private Static void dozip (File file, aeszipfileencrypter encrypter, string pathforentry, string passwd) throws ioexception {If (file. isfile () {pathforentry + = file. getname (); encrypter. add (file, pathforentry, passwd); return;} pathforentry + = file. getname () + file. separator; For (File sub File: file. listfiles () {dozip (subfile, encrypter, pathforentry, passwd );}} /*** extract the specified compressed file to the specified directory using the given password * @ Param infile specify the ZIP file * @ Param outdir unzip directory * @ Param passwd unzip password */public static void unzip (string infile, string outdir, string passwd) {file outdirectory = new file (outdir); If (! Outdirectory. exists () {outdirectory. mkdir ();} aesdecrypter decrypter = new aesdecrypterbc (); aeszipfiledecrypter zipdecrypter = NULL; try {zipdecrypter = new cipher (new file (infile), decrypter); Cipher. charset = "UTF-8";/*** get all the entries in the ZIP file. However, unlike in JDK, the directory is not regarded as entry *. You need to create a folder, entry. the isdirectory () method is also not applicable. I don't know if I am using an error * folder issue handling may be poor */list <extzipentry> entrylist = zipdecrypter. Getentrylist (); For (extzipentry entry: entrylist) {string ename = entry. getname (); string dir = ename. substring (0, ename. lastindexof (file. separator) + 1); file extractdir = new file (outdir, DIR); If (! Extractdir. exists () {fileutils. forcemkdir (extractdir);}/*** extract file */file extractfile = new file (outdir + file. separator + ename); zipdecrypter. extractentry (entry, extractfile, passwd) ;}} catch (ioexception e) {e. printstacktrace ();} catch (dataformatexception e) {e. printstacktrace ();} finally {try {zipdecrypter. close ();} catch (ioexception e) {e. printstacktrace () ;}}/ *** test * @ Param ARGs */public static void main (string [] ARGs) {/*** compression test * can be uploaded to a file or directory * // zip ("M: \ zip \ test \ BB \ A \ t.txt ", "m: \ zip \ test \ temp1.zip", "zyh"); // zip ("M: \ zip \ test \ BB", "m: \ zip \ test \ temp2.zip "," zyh "); unzip (" M: \ zip \ test \ temp2.zip "," m: \ zip \ test \ Temp "," zyh ");}}
There are two methods to compress multiple files (the first method is not tried ):
(1) pre-compress multiple files into ZIP files, and then call enc. addall (inzipfile, password); add multiple ZIP files.
(2) Call enc. Add (infile, password) cyclically for the files to be compressed. The same password is used each time.
The project after the source code is modified can be downloaded from the blog mentioned above or modified by referring to the blog. It is actually very easy. After all, there are only a few changes.
In addition, my csdn download channel also uploads the modified source code and jar package. You can also download it there.
Modify record
Reference http://xjlsgcjdtc.iteye.com/blog/1439514
The files to be modified include:
- Extzipoutputstream
- Extzipentry
- Aeszipfileencrypter
Add a member variable to extzipoutputstream and add two methods:
protected String encoding = "iso-8859-1";public boolean utf8Flg = false;
public void setEncoding(String encoding) {this.encoding = encoding;utf8Flg |= isUTF8(encoding);}
protected boolean isUTF8(String encoding) { if (encoding == null) { // check platform's default encoding encoding = System.getProperty("file.encoding"); } return "UTF8".equalsIgnoreCase(encoding) || "UTF-8".equalsIgnoreCase(encoding); }
Then replace the iso-8859-1 encoding of the extzipoutputstream (about 134 rows and 158 rows) with the encoding format set above
Then, change the code for getting the length of the file name from around 106 lines:
writeShort(entry.getName().getBytes(encoding).length); // file name length
Note that when the file name is in utf8 encoding format, you need to set the general bit mark of the zip package (not clear)
The code is modified as follows:
To modify the extzipentry class, add an overload method based on the initencryptedentry method:
public void initEncryptedEntry(boolean utf8Flag) {setCrc(0); // CRC-32 / for encrypted files it's 0 as AES/MAC checks integritiythis.flag |= 1; // bit0 - encryptedif (utf8Flag) {this.flag |=(1 << 11);}// flag |= 8; // bit3 - use data descriptorthis.primaryCompressionMethod = 0x63;byte[] extraBytes = new byte[11];extraBytes = new byte[11];// extra data header ID for AES encryption is 0x9901extraBytes[0] = 0x01;extraBytes[1] = (byte)0x99;// data size (currently 7, but subject to possible increase in the// future)extraBytes[2] = 0x07; // data sizeextraBytes[3] = 0x00; // data size// Integer version number specific to the zip vendorextraBytes[4] = 0x02; // version numberextraBytes[5] = 0x00; // version number// 2-character vendor IDextraBytes[6] = 0x41; // vendor idextraBytes[7] = 0x45; // vendor id// AES encryption strength - 1=128, 2=192, 3=256extraBytes[8] = 0x03;// actual compression method - 0x0000==stored (no compression) - 2 bytesextraBytes[9] = (byte) (getMethod() & 0xff);extraBytes[10] = (byte) ((getMethod() & 0xff00) >> 8);setExtra(extraBytes);}
Add a parameter and add the following code:
if (utf8Flag) {this.flag |=(1 << 11);}
Of course, do not forget to modify the place where the method is called and pass it to the utf8flag parameter.
There are two places in the aeszipfileencrypter class (in the two add methods) that do not need to be changed elsewhere.
Note: I have passed the test of the above Code. If any of your tests fails, leave a message!
For details about Java basic JDK compression and decompression ZIP files, refer:
Http://blog.csdn.net/zhangyihui1986/article/details/7723649
Download Related Files
Http://download.csdn.net/detail/zhangyihui1986/4415937