package com.daimafans.demo;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import java.io.File;
/**
* 创建带密码的压缩文件
*
* @author daimafans.com
* @date 2018-11-29
*/
public class ZipFileWithPwd
{
public static void main(String[] args) throws ZipException
{
// 生成的压缩文件
ZipFile zipFile = new ZipFile("D:\\test.zip");
ZipParameters parameters = new ZipParameters();
// 压缩方式
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
// 压缩级别
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword("111");
// 要打包的文件夹
File currentFile = new File("D:\\test");
File[] files = currentFile.listFiles();
// 遍历test文件夹下所有的文件、文件夹
assert files != null;
for (File file : files)
{
if (file.isDirectory())
{
zipFile.addFolder(file.getPath(), parameters);
}
else
{
zipFile.addFile(file, parameters);
}
}
}
}