[英]Create password protected zip file in Java without creating it on disk
I need a create zip file.我需要一个创建 zip 文件。 It should be password protected.它应该受密码保护。 I am using lingala jar.我正在使用 lingala jar。 Here is my below.这是我的下面。 Is there a way to do it?有没有办法做到这一点? I even tried zipoutstream, couldn't find a way to add password.我什至尝试了 zipoutstream,找不到添加密码的方法。
@Component
public class FileZipUtils {
@Value("${candela.email.zip.folder}")
private String zipBaseDir;
@Value("${candela.email.zip.encryptionmethod:AES}")
private String encryptionMethod;
@Value("${candela.email.zip.encryptionstrength:KEY_STRENGTH_128}")
private String encryptionStrength;
private ZipParameters zipParameters;
@PostConstruct
private void initializeZipProperties() {
zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(EncryptionMethod.AES);
zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
}
/*
* Creates a zipfile in the zipBaseDir location
*/
public ZipFile createZipFile(String zipFileName,char[] password) {
return new ZipFile(zipBaseDir + "/" + zipFileName,password);
}
/**
* Adds attachment to Zip file
*/
public void addAttachementToZip(ZipFile zipFile, ByteArrayResource fileContentInBytes, String fileName)
throws IOException {
zipParameters.setFileNameInZip(fileName);
zipFile.addStream(fileContentInBytes.getInputStream(), zipParameters);
}
}
The best solution for zip files zip4j
lib. zip 文件zip4j
lib 的最佳解决方案。 ( Github Link ) ( Github 链接)
Features:特征:
I think we need to create file on disk.我认为我们需要在磁盘上创建文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.