Hiya,
I wrote the following code to do it in magnolia 4.4... still needs a bit of
optimization as it's a bit slow at the moment.
I put this code in a magnolia "page" and made it accessible from the backend.
The same code could go in a servlet, or a model class, depending on how you
need it.
Regards from Vienna,
Richard
/**
* Render the given DMS Path into a Zip-File
* @throws IOException
* @throws RepositoryException
* @throws PathNotFoundException
* @throws AccessDeniedException
*/
private void renderToZipFile() throws IOException,
AccessDeniedException, PathNotFoundException, RepositoryException {
getResponse().setContentType("application/zip");
getResponse().setHeader("Content-Disposition", "attachment;
filename=\"magnolia_export.zip\"");
OutputStream os = getResponse().getOutputStream();
ZipOutputStream zos = new ZipOutputStream(os);
zos.setComment("data exported from magnolia cms, 'dms'
repository, path: "+path);
renderZipEntries(zos,root,depth);
zos.finish();
zos.flush();
zos.close();
}
/**
* Render the ZIP-entries
* @param currentNode
* @param currentDepth
* @param zos
* @throws IOException
* @throws RepositoryException
* @throws PathNotFoundException
* @throws AccessDeniedException
*/
private void renderZipEntries(ZipOutputStream zos, Content currentNode,
int currentDepth) throws IOException, AccessDeniedException,
PathNotFoundException, RepositoryException {
if (ItemType.CONTENT.equals(currentNode.getItemType())){
// it's a folder, so only write the folder entry...
if (!currentNode.getHandle().equals(root.getHandle())){
// if we're not the root of the export
ZipEntry ze = new
ZipEntry(getFolderName(currentNode));
zos.putNextEntry(ze);
zos.closeEntry();
}
}
else if
(ItemType.CONTENTNODE.equals(currentNode.getItemType())){
Document doc = new Document(currentNode);
InputStream is = doc.getFileStream();
if (doc!=null && is!=null){
ZipEntry ze = new
ZipEntry(getFileName(currentNode,doc));
zos.putNextEntry(ze);
int byTe = -1;
do {
byTe = is.read();
if (byTe>=0)
zos.write(byTe);
} while (byTe>=0);
is.close();
zos.closeEntry();
}
else
log.warn("Skipping node
'"+currentNode.getHandle()+"' because the document could not be found.");
}
else
log.warn("Skipping node '"+currentNode.getHandle()+"' -
unhandled node type.");
// now render children, recursively
if (currentDepth!=0){
Collection<Content> kids =
ContentUtil.getAllChildren(currentNode);
for (Content kid : kids) {
renderZipEntries(zos, kid, currentDepth-1);
}
}
}
private String getFileName(Content currentNode, Document doc) throws
AccessDeniedException, PathNotFoundException, RepositoryException {
String current = currentNode.getParent().getHandle();
current = StringUtils.removeStart(current, root.getHandle());
current = StringUtils.removeStart(current, "/");
if (!StringUtils.isEmpty(current))
current = current + "/";
String theName = current + doc.getFileName() + "." +
doc.getFileExtension();
int kount = 0;
while (exportedNames.contains(theName)){
kount++;
theName = current + doc.getFileName() + "_" + kount +
"." + doc.getFileExtension();
}
exportedNames.add(theName);
return theName;
}
private String getFolderName(Content currentNode) {
String current = currentNode.getHandle();
current = StringUtils.removeStart(current, root.getHandle());
current = StringUtils.removeStart(current, "/");
current += "/";
exportedNames.add(current);
return current;
}
-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]]
Im Auftrag von Roland Polzer (via Magnolia Forums)
Gesendet: Freitag, 08. Februar 2013 15:55
An: Magnolia User List
Betreff: [magnolia-user] Download DMS Zipfile
Is there a function to download a document tree as ZIP file from DMS?
Or maybe a Groovy script?
Thanks
--
Context is everything:
http://forum.magnolia-cms.com/forum/thread.html?threadId=1bf14983-ca12-4725-9c71-24192a946ba4
----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------
----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------