maven依赖
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
Junit类
/**
* 多文件压缩成zip包,并加入密码
* @throws Exception
*/
@Test
public void zipSecret() throws Exception {
String rootPath = "d:/test/";
//创建压缩文件
ZipFile zipFile = new ZipFile("d:/test/A_zip"+System.currentTimeMillis()+".zip");
ArrayList<File> fileList = new ArrayList<>();
fileList.add(new File(rootPath + "A1530672996753"));
fileList.add(new File(rootPath + "920de35e-8bd9-441a-b789-f192b332a491_muban_test.docx"));
//设置压缩文件参数
ZipParameters zipParameters = new ZipParameters();
//设置压缩方法
zipParameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//设置压缩级别
//DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression
//DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression
//DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed
//DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed
//DEFLATE_LEVEL_ULTRA - Highest compression level but low speed
zipParameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
//设置压缩文件加密
zipParameters.setEncryptFiles(true);
//设置加密方法
zipParameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//设置AES加密强度
zipParameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
//设置密码
zipParameters.setPassword("000000");
zipFile.addFiles(fileList, zipParameters);
System.out.println("ok==>>>");
}
/**
* 解压Zip
* @throws Exception
*/
@Test
public void unzip() throws Exception {
String path = "D:\\test\\A_zip1530683118498.zip";
String destPath = "D:\\test\\" + System.currentTimeMillis() + "\\";
ZipFile zipFile = new ZipFile(path);
if (zipFile.isEncrypted()) {
zipFile.setPassword("000000");
}
zipFile.extractAll(destPath,new UnzipParameters());
File file = new File(destPath);
for (File file1 : file.listFiles()) {
System.out.println(file1.getName());
}
}
最后编辑时间为: July 5th , 2018 at 11:15 am
本文由
wjy 创作,采用
知识共享署名 4.0 国际许可协议进行许可
可自由转载、引用,但需署名作者且注明文章出处