Store files in a zip file with java

8
I would like to do the following: Select a couple of objects in a datagrid Press a invoke button Create for every object a file (Do not store this in the mendix DB) All files are now created Store files in a zip file. Show download form with the zip file Can anybody give me a solution how I can accomplish this?
asked
3 answers
15

Here's another (more specific example) of how you could do it: (please note that I've used the IOUtils package. This is included in the Apache Commons library, which can be downloaded from: http://commons.apache.org/downloads/download_io.cgi It is also included in the default install of Mendix)

    // BEGIN USER CODE
    IMendixObject newFileDocument = Core.create(getContext(), "System.FileDocument");
    zipFile(newFileDocument, files, getContext());
    return true;
    // END USER CODE

// BEGIN EXTRA CODE
private static void zipFile(IMendixObject destinationZipFile, List<system.proxies.FileDocument> filesToZip, IContext context) 
throws IOException {
    File tempFile = File.createTempFile("zipfile", "tmp");
    ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream(tempFile));

    for (FileDocument file : filesToZip) {
        InputStream fileInputStream = Core.getFileDocumentContent(context, file.getMendixObject());
        zipfile.putNextEntry(new ZipEntry(file.getName()));
        IOUtils.copy(fileInputStream, zipfile);
        fileInputStream.close();
    }
    zipfile.close();
    InputStream zipInputStream = new FileInputStream(tempFile);
    Core.storeFileDocumentContent(context, destinationZipFile, zipInputStream);
    tempFile.delete();
}
// END EXTRA CODE
answered
3

On this location you can find an example of how you can create a zip file. Just create an action in your modeler that collects all objects and writes some files in a temp folder. Than build your own action using the sample code from the link and add all the temp files to your zip.
Store the created zip in your object and you're done...

answered
2

Based on this forum post I've created a complete module and added it to the Mendix appstore as "Zip File Document list" module

answered