Unzip an file and store the files and folders to disk

Hello, I would like to unzip a file, which I import in the application. I want to unzip the file and store the files and folders to the disk. How do I create a java action for the same? Kindly advice. Thanks in advance. byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(Core.getFileDocumentContent(getContext(), __ParameterParameter1)); File folder = new File("C:\\outputzip1"); OutputStream outstream = new FileOutputStream(folder); ZipEntry ze = zis.getNextEntry(); while(ze != null){ String filename = ze.getName(); File newFile = new File(outstream + File.separator + filename); new File(newFile.getParent()).mkdirs(); OutputStream outstream1 = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { outstream1.write(buffer, 0, len); } outstream1.close(); ze = zis.getNextEntry(); } --Edit: shows, "java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\outputzip1" "write")" error when executed. What exactly should I do? Thanks.
2 answers

This is a solution that stores to Mendix filedocuments, which is often better. You can patch it to write them to the temp dir. Some snippets to put in your java action (zipfile is inputparameter. ImportFile is a filedocument specialisation)

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;

ZipInputStream zis = new ZipInputStream(Core.getFileDocumentContent(getContext(), zipfile.getMendixObject()));
        ZipEntry ze = zis.getNextEntry();
            String filename = substringAfterLast(ze.getName(), "/");
            ImportFile zipEntryDocument = new ImportFile(getContext());
            // set attributes
            // use intermediate memory stream because storeFileDocumentContent will close the zis stream.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(zis, baos);
               Core.storeFileDocumentContent(this.getContext(), zipEntryDocument.getMendixObject(), new ByteArrayInputStream(baos.toByteArray()));

            ze = zis.getNextEntry();

Extra code

 public static String substringAfterLast(String str, String separator) {
      if (str.isEmpty()) {
          return str;
      if (separator.isEmpty()) {
          return str;
      int pos = str.lastIndexOf(separator);
      if (pos == -1 || pos == (str.length() - separator.length())) {
          return str;
      return str.substring(pos + separator.length());

Edit 1: ImportFile is a specific mendix entity that inherits from filedocument. Replace it with your own entity.


Check the how-to's on how to use Java and Mendix. After that try the java.util.zip package.