Creating zip files from selected documents in a table on a button click
Hi All,
I have a requirement of selecting some of the documents from a table and using a "Download" button zipping it and download it. I was referring the code given in below post:
https://collaborate.pega.com/question/how-create-zip-archive-and-attach-case -
but here not sure how to send logical_zip file back to browser neither sure where tools.sendFile is sending the file back. Please help me understand this as I am a new Pega developer. I tried putting up the logs in addition but not sure if zip file is getting created properly. Below is the code which I have tried using:
Hi All,
I have a requirement of selecting some of the documents from a table and using a "Download" button zipping it and download it. I was referring the code given in below post:
https://collaborate.pega.com/question/how-create-zip-archive-and-attach-case -
but here not sure how to send logical_zip file back to browser neither sure where tools.sendFile is sending the file back. Please help me understand this as I am a new Pega developer. I tried putting up the logs in addition but not sure if zip file is getting created properly. Below is the code which I have tried using:
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("dragDropFileUpload").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(); if(strOutputPRFileName != "") { byte [] data = Base64Util.decodeToByteArray(strOutputPRFileName); String strOutputFileName = objOneRowPage.getProperty("pyFileName").getStringValue();
oLog.infoForced("File Stream: "+ strOutputPRFileName); oLog.infoForced("File Output: "+ strOutputFileName); 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) { System.out.println("Cannot create archive"); throw new PRRuntimeException("Cannot create archive"); } } i++; } //Send file to client String s = tools.getLocalizedTextForString("pyCaption","AllDocuments"); HashStringMap aMap = new HashStringMap(); aMap.put("ContentDisposition", "attachment; filename="+s); aMap.put("ContentType", "application/zip"); try{ logical_zip.finish(); baos.close(); oLog.infoForced("File ZIP: " + s + ".zip"); tools.sendFile(baos.toByteArray(),s+".zip",false,null,true); logical_zip.close();
} catch(Exception e) { throw new PRRuntimeException("Error while sending:"+e.getMessage()); }