Download Case Attachments as zip file in Pega

Share Post

Pega Email Bot : requirements

Requirement:
1.Pega Personnel Edition

 

Activity ScreenShot:



Java Code in Step 6:

 

java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();

org.apache.commons.compress.archivers.ArchiveOutputStream logical_zip = null;

try{

logical_zip = new org.apache.commons.compress.archivers.ArchiveStreamFactory().createArchiveOutputStream(“zip”, baos);

} catch(Exception e) {

throw new PRRuntimeException(“Cannot initialize archive”);

}

 

 

ClipboardProperty cpResults = tools.findPage(“FileAttachmentsPage”).getProperty(“pxResults”);

int i = 1;

for (Iterator instPgList = cpResults.iterator(); instPgList.hasNext();) {

 

 

ClipboardProperty objOneRow = (ClipboardProperty) instPgList.next();

ClipboardPage objOneRowPage = objOneRow.getPageValue();

oLog.infoForced(“FileName Page: “+ objOneRowPage );

 

 

String strOutputPRFileName = objOneRowPage.getProperty(“pyAttachStream”).getStringValue();

byte [] data = Base64Util.decodeToByteArray(strOutputPRFileName);

oLog.info(data);

 

String strOutputFileName = objOneRowPage.getProperty(“pxAttachName”).getStringValue();

 

 

oLog.infoForced(“File Stream: “+ strOutputPRFileName);

  PRFile prDestinationFile = new PRFile(strOutputPRFileName);

 

 

PRInputStream prOut = null;

 

try {

        java.util.zip.ZipEntry entry = new java.util.zip.ZipEntry(strOutputFileName);

  logical_zip.putArchiveEntry(new  org.apache.commons.compress.archivers.zip.ZipArchiveEntry(strOutputFileName));

  logical_zip.write(data);

        logical_zip.closeArchiveEntry();

      } catch(Exception e) {

        throw new PRRuntimeException(“Cannot create archive”);

    }

  i++;

}

//Send file to client

String s = tools.getLocalizedTextForString(“pyCaption”,”CaseAttachments”);

HashStringMap aMap = new HashStringMap();

aMap.put(“ContentDisposition”, “attachment; filename=”+s);

aMap.put(“ContentType”, “application/zip”);

try{

  logical_zip.finish();

  baos.close();

  String strFileData = Base64Util.encodeToString(baos.toByteArray());

  ClipboardPage myPage = tools.findPage(“FileAttachmentsPage”);

  myPage.getProperty(“SteamAttach”).setValue(strFileData);

  tools.sendFile(baos.toByteArray(),s+”.zip”,false,null,true);

  

} catch(Exception e) {

  throw new PRRuntimeException(“Error while sending:”+e.getMessage());

}

please let me know if you are unable to implement or need help, you can comment below or connect/contact me in linkedin :   https://www.linkedin.com/in/bibhash-kalita-b477aa9b/

2 Comments

  1. Hi bhinhash,
    I need your inputs regarding downloading case attachments. Tried the above but facing errors

Leave a Comment

Your email address will not be published. Required fields are marked *