Download all attachments


<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
	<xp:span style="color:rgb(192,0,0)">Sorry, no attachments found in this document!</xp:span>
	<xp:this.beforeRenderResponse><![CDATA[#{javascript:var downloadDocument:NotesDocument = database.getDocumentByUNID(context.getUrl().getParameter("documentUNID"));
var attachments:java.util.Vector = session.evaluate("@AttachmentNames", downloadDocument);
// If there are no attachments then STOP!
if (attachments == null || (attachments.size() == 1 && attachments.get(0).toString().trim().equals(""))) {
	this.setRendered(true); // Show the XPage
	return;
}

var externalContext:javax.faces.context.ExternalContext = facesContext.getExternalContext();
var response:javax.servlet.http.HttpServletResponse = externalContext.getResponse();

// Get the name of the zip file to be shown in download dialog box
var zipFileName = context.getUrl().getParameter("zipFileName");
if (zipFileName == null || zipFileName.equals("")) {
	zipFileName = "AllAttachments.zip";
} else if (!zipFileName.toLowerCase().endsWith(".zip")) {
	zipFileName = zipFileName + ".zip";
}
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);

var outStream:java.io.OutputStream = response.getOutputStream();
var zipOutStream:java.util.zip.ZipOutputStream = new java.util.zip.ZipOutputStream(outStream);
var embeddedObj:NotesEmbeddedObject = null;
var bufferInStream:java.io.BufferedInputStream = null;

// Loop through all the attachments
for (var i = 0; i < attachments.size(); i++) {
	embeddedObj = downloadDocument.getAttachment(attachments.get(i).toString());
	if (embeddedObj != null) {
		bufferInStream = new java.io.BufferedInputStream(embeddedObj.getInputStream());
		var bufferLength = bufferInStream.available();
		var data = new byte[bufferLength];
		bufferInStream.read(data, 0, bufferLength); // Read the attachment data
		var entry:java.util.zip.ZipEntry = new java.util.zip.ZipEntry(embeddedObj.getName());
		zipOutStream.putNextEntry(entry);
		zipOutStream.write(data); // Write attachment into Zip
		bufferInStream.close();
		embeddedObj.recycle();
	}
}

downloadDocument.recycle();
zipOutStream.flush();
zipOutStream.close();
outStream.flush();
outStream.close();
facesContext.responseComplete();}]]>
	</xp:this.beforeRenderResponse>
</xp:view>
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
3 comment(s)Login first to comment...
jeniffer homes
(at 02:03 on 22.07.2016)
I always loved the feature of “Download all attachments” in Gmail.
Grégory DEVISE
(at 03:10 on 23.07.2013)
Really usefull snippet, thank you !

@Ferhat BULUT : You could change content type header replacing "response.setContentType("application/zip");" with "response.setContentType("gzip");"
Ferhat BULUT
(at 16:30 on 16.06.2012)
If you selected "GZip" in "Application Properties -> XPages tab -> HTML Generation -> Compression" property, system zips your file twice. You can download zipped file which is also GZipped (without file extension). It is confused me but I found the reason after a while :)
If you select "None" in related (writed above) property, you can download zip file with actual (attached in the document) file.
Anyway Thanks Naveen Maurya, It is good XSnippet.