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 ;}}