Discussion
PEGA
Pegasystems Inc.
JP
PEGA
Posted: Dec 13, 2023
Last activity: Jan 2, 2024
Last activity: 2 Jan 2024 3:18 EST
create and download password protected zipfile
below java step can be used to download password protected zipfile. zip4j library is required.(https://github.com/srikanth-lingala/zip4j)
//list of files to be compressed in a zip file
ClipboardProperty SendFiles = tools.getPrimaryPage().getProperty("SendFiles");
boolean encrypt = Boolean.valueOf(tools.getParameterPage().getString("encrypt"));
try{
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
//Zip4j--begin Setting password for the ZIP file
net.lingala.zip4j.io.outputstream.ZipOutputStream zos;
if(encrypt){
String password = tools.getParameterPage().getString("password"); //AES only supports key sizes of 16, 24 or 32 bytes.
zos= new net.lingala.zip4j.io.outputstream.ZipOutputStream(baos,password.toCharArray());
}else{
zos= new net.lingala.zip4j.io.outputstream.ZipOutputStream(baos);
}
net.lingala.zip4j.model.ZipParameters zipParameters = new net.lingala.zip4j.model.ZipParameters();
zipParameters.setCompressionMethod(net.lingala.zip4j.model.enums.CompressionMethod.DEFLATE);
if(encrypt){
zipParameters.setEncryptFiles(true);
zipParameters.setEncryptionMethod(net.lingala.zip4j.model.enums.EncryptionMethod.ZIP_STANDARD);
//7zip is ok with AES, however, windows 11 default decompression feature doesn't work if using AES.
//zipParameters.setEncryptionMethod(net.lingala.zip4j.model.enums.EncryptionMethod.AES);
//zipParameters.setAesKeyStrength(net.lingala.zip4j.model.enums.AesKeyStrength.KEY_STRENGTH_128);
}
//Zip4j--end
//read files
for (Iterator it = SendFiles.iterator(); it.hasNext();) {
ClipboardProperty objOneRow = (ClipboardProperty) it.next();
ClipboardPage objFile = objOneRow.getPageValue();
byte [] zipEntryFileData = Base64Util.decodeToByteArray(objFile.getProperty("AttachStream").getStringValue());
String zipEntryFileName = objFile.getProperty("AttachName").getStringValue();
//add to zip file
zipParameters.setFileNameInZip(zipEntryFileName);
zos.putNextEntry(zipParameters);
java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(zipEntryFileData);
byte[] buffer = new byte[4096];
int length;
while ((length = bais.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
bais.close();
}
zos.close();
byte[] zipData = baos.toByteArray();
baos.close();
//CJK filename encoding
String urlEncodedFileName = java.net.URLEncoder.encode(downloadFileName, "UTF-8")+".zip";
urlEncodedFileName = "\"" + urlEncodedFileName + "\"" + ";filename*=UTF-8''" + urlEncodedFileName;
//Send file to client
HashStringMap headers = new HashStringMap();
headers.put("ContentDisposition", "attachment; filename="+urlEncodedFileName);
headers.put("ContentType", "application/zip");
downloadFileName += ".zip";
tools.sendFile(zipData,downloadFileName,false,headers,true);
} catch (java.lang.Exception e) {
downloadFileName = "";
e.printStackTrace();
throw new java.lang.RuntimeException(e);
}
***Edited by Moderator Marije to add DKS Capability tags***
To see attachments, please log in.