What is alternative for: Core.getFileDocumentContentAsFile?

3
I use this method to retrieve an uploaded zipfile and unzip in java using: ZipFile zipfile = new ZipFile(Core.getFileDocumentContentAsFile.... However, this method is deprecated. Is there an alternative that allows me to retrieve the file without having to create a local file? Which is what I need to do when using File file = new File("localfile"). UPDATE: I don't get it to work yet. Problem is that I don't know how to convert ZipEntry to InputStream which I can use to store entry as Mendix object. ZipInputStream zipis = new ZipInputStream(Core.getFileDocumentContent(this.getContext(), Zipfile.getMendixObject())); while(zipis.available()==1) { ZipEntry entry = zipis.getNextEntry();
asked
2 answers
4

Certain environments don't offer direct file access, which might not always make it possible to give people direct access to the File objects. We've deprecated the method for this reason.

The neat way of interacting with filedocuments via the Java Core API is by using input/outputstreams:

Core.getFileDocumentContent

If you want to read a zip file that's contained in a FileDocument, you should be able to wrap it in a zipinputstream, which will allow you to read the contents without resorting to a File object.

answered
0

Possibly helpful for others. My code that works:

    ZipInputStream zipis = new ZipInputStream(Core.getFileDocumentContent(this.getContext(), inputZip.getMendixObject()));
ZipEntry entry;
while((entry = zipis.getNextEntry()) != null) {
    if (entry.isDirectory()) 
    {
    }
    else 
    {
        Core.getLogger("log").info("files to be created");
        Image img = Image.create(this.getContext());
        img.setName(entry.getName());
        try {
            Core.getLogger("log").info("retrieved filestream");
            IMendixObject fileMendixObject = img.getMendixObject();
            IncomingDoc id = IncomingDoc.initialize(this.getContext(), inputFile.getMendixObject());

            int size;
            byte[] buffer = new byte[2048];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(baos, buffer.length);
            while ((size = zipis.read(buffer, 0, buffer.length))!=-1) {
                bos.write(buffer, 0, size);
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            if (bais!=null){
                Core.storeImageDocumentContent(this.getContext(), fileMendixObject, bais, 60, 60);
            }
            bais.close();
            bos.flush();
            bos.close();

        } catch (ZipException ze) {
            throw new MendixRuntimeException("Zipfile cannot be unzipped", ze);
        } 
    }
}
zipis.close();
return true;
answered