First things first, include maven dependency to download zip4j jar files
1 <dependency> 2 <groupId>net.lingala.zip4j</groupId> 3 <artifactId>zip4j</artifactId> 4 <version>1.3.1</version> 5 </dependency>
Second, write the zip method, either can be a static util method or a private method within a class.
/** * * @param path, the absolute path where the zip file is * @param fileList, a list of File objects that is going to be zipped * @param password, need to be entered for extraction * Create a zip file with password protected * @throws ZipException */ public static void createZipWithPassword(String path, ArrayList<File> fileList, String password) throws ZipException { final ZipFile zipFile = new ZipFile(path); final ZipParameters parameters = new ZipParameters(); parameters.setEncryptFiles(true); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); parameters.setPassword(password); zipFile.createZipFile(fileList, parameters); }
时间: 2024-10-11 13:23:06