ZIP file functionality for Mx7 ?

0
In older Mendix versions one could use the CreateZIP (Mx5) or Zip File Document List (Mx3) modules from the AppStore, however they do not seem to be compatible with Mx7. I failed in getting them working in Mx7, just like some other Java ZIP code of another Mx6 application, since the FileDocument interface has changed considerably over time. Does anybody have some example JAVA code how to get this done?   I am looking for something very basic, just like:  Boolean CreateZipFile(System.FileDocument[] SourceFiles, System.FileDocument ZipFile) Below is some code I have now, but the Mx6 compatible line "IOUtils.write(file.getContents(), zipfile);" fails in Mx7 // This file was generated by Mendix Modeler. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments. package st_common.actions; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.commons.io.IOUtils; import system.proxies.FileDocument; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.systemwideinterfaces.core.IMendixObject; import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.webui.CustomJavaAction; /** * This java action allows you to create a ZIP file from a list of System.FileDocument files */ public class CreateZipFile extends CustomJavaAction<java.lang.Boolean> { private java.util.List<IMendixObject> __SourceFiles; private java.util.List<system.proxies.FileDocument> SourceFiles; private IMendixObject __ZipFile; private system.proxies.FileDocument ZipFile; public CreateZipFile(IContext context, java.util.List<IMendixObject> SourceFiles, IMendixObject ZipFile) { super(context); this.__SourceFiles = SourceFiles; this.__ZipFile = ZipFile; } @Override public java.lang.Boolean executeAction() throws Exception { this.SourceFiles = new java.util.ArrayList<system.proxies.FileDocument>(); if (__SourceFiles != null) for (IMendixObject __SourceFilesElement : __SourceFiles) this.SourceFiles.add(system.proxies.FileDocument.initialize(getContext(), __SourceFilesElement)); this.ZipFile = __ZipFile == null ? null : system.proxies.FileDocument.initialize(getContext(), __ZipFile); // BEGIN USER CODE File tempFile = File.createTempFile("zipfile", "tmp"); ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream(tempFile)); List<String> fileList = new ArrayList<String>(); for (system.proxies.FileDocument file : SourceFiles) { String fileName = file.getName(); if (fileList.contains(fileName)) fileName = file.getMendixObject().getId().toLong() + " " + fileName; fileList.add(fileName); zipfile.putNextEntry(new ZipEntry(fileName)); IOUtils.write(file.getContents(), zipfile); } zipfile.close(); InputStream zipInputStream = new FileInputStream(tempFile); Core.storeFileDocumentContent(this.getContext(), ZipFile.getMendixObject(), zipInputStream); tempFile.delete(); return true; // END USER CODE } /** * Returns a string representation of this action */ @Override public java.lang.String toString() { return "CreateZipFile"; } // BEGIN EXTRA CODE // END EXTRA CODE } Any help is welcome, thanks!
asked
2 answers
3

There seems to be some undocumented API Breaking changes (file.getContents) in 7.5. Change the inner loop

 

		for (system.proxies.FileDocument file : SourceFiles) {
			String fileName = file.getName();
			if (fileList.contains(fileName)) fileName = file.getMendixObject().getId().toLong() + " " + fileName;
			fileList.add(fileName);
			zipfile.putNextEntry(new ZipEntry(fileName));
			InputStream inputStream = Core.getFileDocumentContent(getContext(), file.getMendixObject());
			IOUtils.copy(inputStream, zipfile);
			zipfile.closeEntry();
		}

 

answered
3

Thanks for your feedback!

 

The version in the app store has been updated: https://appstore.home.mendix.com/link/app/2186/First-Consulting/CreateZIP

answered