Encrypt and decrypt files and directories using zip4j

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.