Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decompress a gzipped data in a byte array?

I have a class which has a method that is receiving an object as a parameter. This method is invoked via RMI.

public RMIClass extends Serializable {
    public RMIMethod(MyFile file){
        // do stuff
    }
}

MyFile has a property called "body", which is a byte array.

public final class MyFile implements Serializable {

    private byte[] body = new byte[0];
    //.... 

    public byte[] getBody() {
        return body;
    }
    //....
}

This property holds the gzipped data of a file that was parsed by another application.

I need to decompress this byte array before performing further actions with it.

All the examples I see of decompressing gzipped data assume that I want to write it to the disk and create a physical file, which I do not.

How do I do this?

Thanks in advance.

like image 675
rshepherd Avatar asked Nov 06 '08 21:11

rshepherd


2 Answers

Wrap your byte array with a ByteArrayInputStream and feed it into a GZipInputStream

like image 58
basszero Avatar answered Oct 21 '22 10:10

basszero


Look at those samples, and wherever they're using FileOutputStream, use ByteArrayOutputStream instead. Wherever they're using FileInputStream, use ByteArrayInputStream instead. The rest should be simple.

like image 44
Jon Skeet Avatar answered Oct 21 '22 10:10

Jon Skeet