Perspective download zip file with several files inside

As an exercise to myself, I wanted to see if I could do the same thing with the standard Java library. It produces a jarray as output automatically, and should be more efficient in Jython since it is java-based.

import java.util.zip.ZipOutputStream as ZipOutputStream
import java.io.ByteArrayOutputStream as ByteArrayOutputStream
import java.util.zip.ZipEntry as ZipEntry

def datasets_to_zip(filenames, datasets):
    byte_stream = ByteArrayOutputStream()
    zip_stream = ZipOutputStream(byte_stream)
    for file_name, dset in zip(filenames, datasets):
        zip_stream.putNextEntry(ZipEntry(file_name))
        zip_stream.write(system.dataset.toCSV(dset))
    zip_stream.close()
    return byte_stream.toByteArray()
7 Likes