Import files from .zip

0
Good afternoon,   I'm trying to import an .xml and .dtd file into my (first!) app. These files can be found in a .zip via a direct url. I've managed to manually import the .xml document into my app and map it to my domain model. Is it possible and if so: how? to import the 2 files directly from the url and store them as filedocuments?   regards, Chris
asked
3 answers
5

You can store the zip in a filedocument and create a java action to unzip it into filedocuments.

ImportFile  is a specialization of filedocument

parameter

zipfile: filedocument

imports

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;

User code

		ZipInputStream zis = new ZipInputStream(Core.getFileDocumentContent(getContext(), zipfile.getMendixObject()));
		ZipEntry ze = zis.getNextEntry();
		while(ze!=null){
			String filename = substringAfterLast(ze.getName(), "/");
			ImportFile zipEntryDocument = new ImportFile(getContext());
			zipEntryDocument.setName(filename);
			// use intermediate memory stream because storeFileDocumentContent will close the zis stream.
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			IOUtils.copy(zis, baos);
			Core.getLogger("Unzipper").debug("store import file " + zipEntryDocument.getName());
		        Core.storeFileDocumentContent(this.getContext(), zipEntryDocument.getMendixObject(), new ByteArrayInputStream(baos.toByteArray()));
			zipEntryDocument.commit();
			ze = zis.getNextEntry();
		}
		zis.closeEntry();
		zis.close();
		return true;

 

EDIT 2: you can download a 10.6.3 module from this link

answered
3

You can also have a look at the Community Commons module from the appstore. This contains the 'storeURLtoFileDocument' action.

answered
-1

Hi Chris,

If you already have an import mapping, it is not a big step further to create an export mapping to map the xml to a filedocument. For this you can use the 'Export With Mapping' action in a microflow and store the output as a FileDocument.

 

answered