Examples of Java methods for automatically compressing and encrypting files, java examples

Source: Internet
Author: User

Examples of Java methods for automatically compressing and encrypting files, java examples

This example describes how to implement automatic File compression and encryption in Java. We will share this with you for your reference. The details are as follows:

Feature: automatic compression and Encryption

/***** @ Title: zipFilesAndEncrypt * @ Description: compresses the files in the specified path to the specified zip file and encrypts the files with the specified password. If the password is empty, no encryption protection * @ param srcFileName path of the file to be compressed * @ param zipFileName zip file name * @ param password encryption password * @ return * @ throws Exception */public void zipFilesAndEncrypt (String srcFileName, string zipFileName, String password) throws Exception {ZipOutputStream outputStream = null; System. out. println ("Enter test class"); if (StringUtils. isEmpty (srcFileName) | StringUtils. isEmpty (zipFileName) {log. error ("the requested compression path or file name is incorrect"); return;} try {ZipParameters parameters = new ZipParameters (); parameters. setCompressionMethod (Zip4jConstants. COMP_DEFLATE); parameters. setCompressionLevel (Zip4jConstants. DEFLATE_LEVEL_NORMAL); if (! StringUtils. isEmpty (password) {parameters. setEncryptFiles (true); parameters. setEncryptionMethod (Zip4jConstants. ENC_METHOD_AES); parameters. setAesKeyStrength (Zip4jConstants. AES_STRENGTH_256); parameters. setPassword (password);} ArrayList <File> filesToAdd = new ArrayList <File> (); File file = new File (srcFileName); File [] files = new File [0]; if (file. isDirectory () {files = file. listFiles (); for (int I = 0; I <files. length; I ++) {filesToAdd. add (new File (srcFileName + files [I]. getName (); System. out. println ("file name:" + files [I]. getName () ;}} else {filesToAdd. add (new File (srcFileName + file. getName ();} ZipFile zipFile = new ZipFile (srcFileName + zipFileName + ". zip "); zipFile. addFiles (filesToAdd, parameters);} catch (Exception e) {System. out. println ("File compression error"); log. error ("File compression error", e); throw e ;}}

Related Article

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.