How to decompress a gzipped data in a byte array?

断了今生、忘了曾经 提交于 2019-11-26 14:46:49

问题


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.


回答1:


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




回答2:


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.




回答3:


Why don't you create your own class that extends OutputStream or , whatever is the archive writing to ?




回答4:


If you want to write to a ByteBuffer you can do this.

private static void uncompress(final byte[] input, final ByteBuffer output) throws IOException
    {
        final GZIPInputStream inputGzipStream = new GZIPInputStream(new ByteArrayInputStream(input));
        Channels.newChannel(inputGzipStream).read(output);
    }


来源:https://stackoverflow.com/questions/270268/how-to-decompress-a-gzipped-data-in-a-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!