Export Documents as ZIP in wdk applications
January 14th, 2010 by Praveena Killamsetty
We had a requirement in webtop where the client needed to export the selected documents as a zipped folder into the local client machine. It was interesting and time consuming since we did not want to write any Business Objects for this customization. I looked up a lot about this but couldn’t find a working solution. I am posting it here since it might be handy to others looking for something similar to this.
The user selects the documents needed to be exported and clicks on the menu option to export as zip.Then a jsp page pops up prompting the user to put in a name for the zipped folder.
After the user puts in a name and clicks Finish a page opens up with the zipped folder as link which the user needs to click to download it into the local client machine.
When clicked on the link the user is prompted for a location to save the zip folder and then the folder gets saved into the given location.
I followed the following steps to achive this:
Step 1: Put in all the selected objectIds in an ArrayList.
Step 2: Use the ContentTransferConfig object to get the ContentServer location and create a temp file in that location.
Step 3:Get the objects from the previously stored objectIds and and add them as zip entries to the temporary file created.
Step 4: Use the HTTPTransportManager to push the data and show up the links to download into the local machine.
Code:
Component comExport = getContainedComponent();
ArrayList comArray = new ArrayList();
ArrayList idArray = new ArrayList();
ArgumentList argList=new ArgumentList();
comArray = super.getContainedComponents();
Text zipControl=(Text) getContainedComponent().getControl(“zip_name”);
String zipName=zipControl.getValue();
for(int i=0; i<comArray.size(); i++)
{
comExport= (Component)comArray.get(i);
argList = comExport.getInitArgs();
idArray.add(argList.get(“objectId”));
}
ContentTransferConfig ctConfig = ContentTransferConfig.getConfig();
File tempFolderAppServer = ctConfig.getServerContentLocation();
File tempFile = File.createTempFile((new StringBuilder()).append(“mytempzip_”).toString(), “”, tempFolderAppServer);
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
ZipOutputStream zos = new ZipOutputStream(fileOutputStream);
for (int i = 0; i < idArray.size(); i++)
{
IDfSysObject sysObject = (IDfSysObject) getDfSession().getObject(new DfId(idArray.get(i).toString()));
String strObjectFormat=sysObject.getFormat().getDOSExtension();
ZipEntry zipAdd = new ZipEntry(sysObject.getObjectName()+”.”+strObjectFormat);
zos.putNextEntry(zipAdd);
ByteArrayInputStream stream = sysObject.getContent();
byte[] buffer = new byte[10240];
while (true)
{
int len = stream.read(buffer, 0, buffer.length);
if (len <= 0)
break;
zos.write(buffer, 0, len);
}
stream.close();
}
HttpTransportManager manager = HttpTransportManager.getManager();
List ids = new ArrayList(1);
com.documentum.web.contentxfer.http.HttpTransportManager.Content content=null;
content = new com.documentum.web.contentxfer.http.HttpTransportManager.Content(tempFile, (new StringBuilder()).append(zipName).toString(), “zip”, tempFile.getParent(), 2);
idArray.add(manager.addOutgoing(content));
String contextPath = ((HttpServletRequest)getPageContext().getRequest()).getContextPath();
manager.setClientDownloadEvent(idArray, getTopForm(), contextPath);
zos.close();
fileOutputStream.close();
tempFile.deleteOnExit();