Because of the project suddenly to send compressed encrypted message attachments, so from the Internet to read some information said zip4j Open source framework is better, support for Chinese is also better, so from the Internet to find a code case! I wrote a write, now posted out, easy to use later when you want to find
1.
1 packagecom.fenghao.zip;2 3 Importjava.io.File;4 5 Importjava.util.ArrayList;6 Importjava.util.Collections;7 8 Importnet.lingala.zip4j.core.ZipFile;9 Importnet.lingala.zip4j.exception.ZipException;Ten Importnet.lingala.zip4j.model.ZipParameters; one Importnet.lingala.zip4j.util.Zip4jConstants; a - - the /** - * Compress and Encrypt files - * Extract and Decrypt files - * @authorFenghao + * - */ + public classCompressutils { a at /** - * Unzip the encrypted compressed file - * @paramZipFile - * @paramdest - * @parampasswd - * @throwszipexception in */ - public voidUnZip (File zipfile,string dest,string passwd)throwszipexception{ toZipFile zfile=NewZipFile (zipfile); + //Zfile.setfilenamecharset ("GBK");//in the GBK system, you need to set - if(!Zfile.isvalidzipfile ()) { the Throw NewZipexception ("the Compressed file is not legal and may be Corrupted!") "); * } $File file=NewFile (dest);Panax Notoginseng if(file.isdirectory () &&!file.exists ()) { - file.mkdirs (); the } + if(zfile.isencrypted ()) { a Zfile.setpassword (passwd.tochararray ()); the } + Zfile.extractall (dest); - } $ /** $ * Compress files and encrypt - * @paramsrc - * @paramdest the * @param is - * @parampasswdWuyi */ the public voidZip (String src,string dest,Booleanis,string Passwd) { -File srcfile=NewFile (src); wu //Create a target file -String Destname =builddestfilename (srcfile, dest); aboutZipparameters par=Newzipparameters (); $ Par.setcompressionmethod (zip4jconstants.comp_deflate); - Par.setcompressionlevel (zip4jconstants.deflate_level_normal); - if(passwd!=NULL){ -Par.setencryptfiles (true); a Par.setencryptionmethod (zip4jconstants.enc_method_standard); + Par.setpassword (passwd.tochararray ()); the } - Try { $ZipFile zipfile=NewZipFile (destname); the if(srcfile.isdirectory ()) { the if(!is ) { thefile[] Listfiles =srcfile.listfiles (); theArraylist<file> temp=NewArraylist<file>(); - Collections.addall (temp, listfiles); in zipfile.addfiles (temp, par); the } the Zipfile.addfolder (srcfile, par); about}Else{ the zipfile.addfile (srcfile, par); the } the}Catch(zipexception E) { + e.printstacktrace (); - } the Bayi the } the /** - * Target file name - * @paramSrcfile the * @paramdest the * @return the */ the publicString builddestfilename (File srcfile,string dest) { - if(dest==NULL){//when the target path is not given the if(srcfile.isdirectory ()) { theDest=srcfile.getparent () +file.separator+srcfile.getname () + ". zip"; the}Else{94String filename=srcfile.getname (). substring (0,srcfile.getname (). lastIndexOf (".")); theDest=srcfile.getparent () +file.separator+filename+ ". zip"; the } the}Else{98Createpath (dest);//Creation of Paths about if(dest.endswith (file.separator)) { -String filename= "";101 if(srcfile.isdirectory ()) {102Filename=srcfile.getname ();103}Else{104Filename=srcfile.getname (). substring (0, srcfile.getname (). lastIndexOf (".")); the }106dest+=filename+ ". zip";107 }108 }109 returndest; the }111 /** the * Path Creation113 * @paramdest the */ the Private voidCreatepath (String Dest) { theFile destdir=NULL;117 if(dest.endswith (file.separator)) {118Destdir=NewFile (dest);//given is the path when119}Else{ -Destdir=NewFile (dest.substring (0, Dest.lastindexof (file.separator)));121 }122 if(!destdir.exists ()) {123 destdir.mkdirs ();124 } the }126 127 @org. junit.test - public voidTest () {129String src= "/home/fenghao/document/book class Material/maven actual combat HD Scan full Version. pdf"; theString dest= "/home/fenghao/zip/maven/123.zip";131Zip (src, dest,true, "123456"); the }133}
2, because I created the Maven project, so the jar dependencies are also posted out!
<!--https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j--
<!--support for compressed files and encryption--
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
zip4j simple use of open source jar packages