Reputation: 388
I need a create zip file. It should be password protected. I am using 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.
@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);
}
}
Upvotes: 0
Views: 3309
Reputation: 366
The best solution for zip files zip4j
lib.
(Github Link)
Features:
Upvotes: 1