JSP to download OpenCms resources as a ZIP
What often bugged me is that I can upload a set of files into OpenCms as a ZIP file but can not download a tree of files as such (without the use of the database export feature). I had hacked together this little JSP some time ago. It allows you to dynamically download a set of OpenCms resources as a ZIP file.
The JSP has no interface since I mainly use it for development. Simply create a JSP somewhere in the OpenCms VFS and then open the file via the browser. E.g.:
“http://workplace.com/system/createzip?source=/system/modules/foo&filename=foo-module.zip”
The source parameter specifies which directory to zip up and the filename parameter which filename to use for the generated file. You can only zip up resources with a total size of 10 megabytes. This is mainly to prevent OutOfMemory errors to happen. You can easily increase the size by altering the variable maxSourceSize to any value you need.
If you find any bugs or want to give feedback simply leave a comment or send me an email.
<%@page import="org.opencms.util.*,
org.opencms.jsp.*,
org.opencms.file.*,
java.util.*,
java.io.*,
java.util.zip.*
" %><%
// Maximum filesize to prevent OutOfMemory errors
long maxSourceSize = 1024*1024*10; // 10 Megabytes
// The filename to use as output
String filename = request.getParameter("filename");
// The directory to zip
String source = request.getParameter("source");
if (CmsStringUtil.isEmpty(filename) || CmsStringUtil.isEmpty(source )) {
// Ensure that all parameters are given
throw new IllegalArgumentException("Please specify the 'source' and the 'filename' parameter");
}
if (!source.endsWith("/")) { source = source + "/"; }
CmsJspActionElement cmsa = new CmsJspActionElement(pageContext,request,response);
CmsObject cmso = cmsa.getCmsObject();
List zipResources = new ArrayList();
// Collect all the resources
CmsResource sourceResource = cmso.readResource(source);
if (!sourceResource.isFolder()) {
throw new IllegalArgumentException("Please specify a folder to zip");
}
List cmsResources = cmso.readResources(source,CmsResourceFilter.DEFAULT);
for (Iterator i = cmsResources.iterator(); i.hasNext();) {
zipResources.add(i.next());
}
// Create the zip file
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zipfile = new ZipOutputStream(bos);
long currentSize = 0;
for (Iterator i = zipResources.iterator(); i.hasNext(); ) {
CmsResource res = (CmsResource) i.next();
currentSize += res.getLength();
if (currentSize > maxSourceSize) {
// Ensure that we don't zip up too much
throw new RuntimeException("Source size is bigger than: " + (maxSourceSize/1024) + " kilobytes");
}
String sourcePath = cmso.getRequestContext().getSitePath(res);
String targetPath = sourcePath;
if (targetPath.startsWith(source)) {
targetPath = targetPath.substring(source.length());
}
//out.println("Zipping " + sourcePath + " as " + targetPath + "<br/>");
if (res.isFile()) {
ZipEntry zipentry = new ZipEntry(targetPath);
zipfile.putNextEntry(zipentry);
CmsFile file = cmso.readFile(res);
zipfile.write(file.getContents(),0,file.getLength());
}
}
zipfile.close();
byte[] content = bos.toByteArray();
response.setContentType("application/octet-stream");
response.setContentLength((int)content.length);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"" );
response.getOutputStream().write(content,0,content.length);
response.getOutputStream().flush();
%>
Tags: Development, Java, JSP, OpenCms, Sourcecode, ZIP
