Talk less, look directly at the tool class:
PackageCom.ilucky.zip4j.util;ImportJava.io.File;ImportNet.lingala.zip4j.core.ZipFile;ImportNet.lingala.zip4j.exception.ZipException;ImportNet.lingala.zip4j.model.ZipParameters;Importnet.lingala.zip4j.util.Zip4jConstants;/** * @author iluckysi * @since 20150723 * * Public class zip4jutil { PrivateString Srcpath;PrivateString Dstpath;PrivateString Password ="123456"; PublicStringGetsrcpath() {returnSrcpath; } Public void Setsrcpath(String Srcpath) { This. Srcpath = Srcpath; } PublicStringGetdstpath() {returnDstpath; } Public void Setdstpath(String Dstpath) { This. Dstpath = Dstpath; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; }/** * Encryption * supports encrypting all files in a file or directory. * 1. A file: D:\\test\\src.zip. * 2 a directory: D:\\TEST\\SRC * @return boolean */ Public Boolean Encrypt() {Try{if(!NewFile (Srcpath). Exists ()) {System.out.println ("Source path does not exist"+srcpath);return false; } zipparameters parameters =NewZipparameters (); Parameters.setencryptfiles (true); Parameters.setencryptionmethod (Zip4jconstants.enc_method_aes); Parameters.setaeskeystrength (zip4jconstants.aes_strength_256); Parameters.setpassword (Password.tochararray ()); File Srcfile =NewFile (Srcpath); ZipFile destfile =NewZipFile (Dstpath);if(Srcfile.isdirectory ()) {Destfile.addfolder (srcfile, parameters); }Else{destfile.addfile (srcfile, parameters); } System.out.println ("Successfully encrypted files");return true; }Catch(Exception e) {System.out.println ("Encrypting file has an exception:"+E);return false; } }/** * Decryption * Supports extracting an encrypted file under a specified directory. * @return boolean */ Public Boolean Decrypt() {Try{if(!NewFile (Srcpath). Exists ()) {System.out.println ("Source path does not exist"+srcpath);return false; } ZipFile srcfile =NewZipFile (Srcpath); Srcfile.setfilenamecharset ("GBK"); Srcfile.setpassword (Password.tochararray ()); Srcfile.extractall (Dstpath); System.out.println ("Successfully decrypted file");return true; }Catch(Zipexception e) {System.out.println ("The decryption file has an exception:"+E);return false; } }}
Then look at the test class:
PackageCom.ilucky.zip4j.util;/** * @author iluckysi * @since 20150723 * * Public class maintest { Public Static void Main(string[] args) {//encryption.Zip4jutil Zip4jutil =NewZip4jutil (); Zip4jutil.setsrcpath ("D:\\test\\src.zip"); Zip4jutil.setdstpath ("D:\\test\\dst.zip"); Zip4jutil.setpassword ("123"); Zip4jutil.encrypt ();//decryption.Zip4jutil.setsrcpath ("D:\\test\\dst.zip"); Zip4jutil.setdstpath ("d:\\test\\"); Zip4jutil.setpassword ("123");//zip4jutil.decrypt ();}}
Finally look at the Pom file:
<dependency> <groupId>net.lingala.zip4j</groupId> <artifactId>zip4j</artifactId> <version>1.3.2</version> </dependency>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Encrypt and decrypt files and directories using zip4j