我想用密码保护档案压缩文件。我有这样的小鬼:
public class ZipArchiverAdapter implements ZipArchiverPort {
private static final Logger log = LoggerFactory.getLogger(ZipArchiverAdapter.class);
@Override
public void compressFilesWithPassword(List<String> fileToZipPaths, String password) throws IOException {
String sourcePath = System.getProperty("java.io.tmpdir") + "supportPackage";
String destPath = sourcePath + ".zip";
System.out.println("Destination " + destPath);
ZipFile zipFile = new ZipFile(destPath, password.toCharArray());
for(String f: fileToZipPaths) {
zipFile.addFile(new File(f), getParameters());
}
}
private ZipParameters getParameters() {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
zipParameters.setCompressionLevel(CompressionLevel.ULTRA);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
zipParameters.setEncryptFiles(true);
zipParameters.setIncludeRootFolder(true);
return zipParameters;
}
}
这段代码有效。存档用密码压缩。唯一的问题是任何不知道密码的人都可以打开生成的 zip 文件并查看里面有什么文件。当您单击文件时,它将需要您提供密码。但我想用密码隐藏 zip 中的文件。这可以使用任何 zip 程序来实现。我只是不知道该怎么做zip4j
。我检查了文档:
http ://www.lingala.net/zip4j.html
https://github.com/srikanth-lingala/zip4j
但没有找到答案。
请帮忙