Java密码压缩工具 zip4j
这个工具跟其他的比,好处就是 支持带密码的压缩
官网地址:http://www.lingala.net/zip4j.html
github地址:https://github.com/srikanth-lingala/zip4j
maven:https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j
Maven
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.6.4</version>
</dependency>
gradle
compile group: 'net.lingala.zip4j', name: 'zip4j', version: '2.6.4'
例子:
public static void compressFile(File srcFile, File destFile, String password) {
try{
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
ZipFile zipFile = new ZipFile(destFile, password.toCharArray());
zipFile.addFile(srcFile, zipParameters);
}catch ( Exception e ){
throw new BusinessException("压缩文件失败", e);
}
}
如报错:
net.lingala.zip4j.exception.ZipException: Zip headers not found.
Probably not a zip file or a corrupted zip file
可以删除目标文件,或者目标文件是不合法的zip文件
具体其他用法github上有更多例子,也可以看源码
ps:才知道 log4j,zip4j的4j 是4java的意思…