Using ZipInputStream with Javascript

I am trying to save/load some ROIs to a zip file using JS, in a similar fashion to what the ROI manager does.

I have successfully managed to save the ROIs using the following (note ROIs are stored in an Array called ROIs).

var sd = new SaveDialog("Save ROIs to file", IJ.getDirectory("image"), "ROIs", ".roi");
var zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(sd.getDirectory() + sd.getFileName())));

var out = new DataOutputStream(new BufferedOutputStream(zos));
var re = new RoiEncoder(out);

for (var r=0; r<ROIs.length; r++)
        zos.putNextEntry(new ZipEntry("ROI__"+r+"__Frame__"+(frame+1)+"__.roi"));

If I open the zip file I can see the single .roi files, and if I open them in Fiji the ROI is where it is supposed to be!

Now, I want to programmatically read the ROIs back. I use the following code

var infile = new FileInputStream(fileName);
var instream = new ZipInputStream(infile);
var entry = instream.getNextEntry();

var buf, len;

while (entry!=null) 
        name = entry.getName(); // This contains the correct .roi filename
        if (name.endsWith(".roi"))
                out = new ByteArrayOutputStream();
                while ((len = > 0) 
                        out.write(buf, 0, len); 
            entry = instream.getNextEntry(); 

However, I get an error saying that the read method of ZipInputStream cannot be invoked with the passed parameters.

I guess the problem is that is expecting an array of bytes. I tried defining buf as

var buf = new Byte(0);

Or even

var buf = new Uint8Array();

But no luck…

Anyone can help?

I found the solution!

The buffer needs to be declared as such:

var ByteArray = Java.type("byte[]");
var buf = new ByteArray(1024);
1 Like