ketan
ketan

Reputation: 2904

How to decompress lzo byte array using java-lzo library?

I'm trying to decompress compressed byte array using java-lzo library. I'm following this reference.

I added below maven dependency to pom.xml -

<dependency>
        <groupId>org.anarres.lzo</groupId>
        <artifactId>lzo-core</artifactId>
        <version>1.0.5</version>
</dependency>

I created one method which accepts lzo compressed byte array and destination byte array length as a argument.

Program :

private byte[] decompress(byte[] src, int len) {
    ByteArrayInputStream input = new ByteArrayInputStream(src);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
    lzo_uintp lzo = new lzo_uintp(len);
    LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
    LzoInputStream stream = new LzoInputStream(input, decompressor);

    try {
        int data = stream.read();
        while (data != -1) {
            out.write(data);
            data = stream.read();
        }
        out.flush();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return out.toByteArray();
}

I got stuck at one point because stream.read() always returns a "-1". I checked input array it is filled with data. Further I checked using stream.available() method but this method also returns always "0" in my case. But If I checked to InputStream like input.available() then the return value is length of array.

Error is same just like I said it is returning "-1" -

java.io.EOFException
at org.anarres.lzo.LzoInputStream.readBytes(LzoInputStream.java:183)
at org.anarres.lzo.LzoInputStream.readBlock(LzoInputStream.java:132)
at org.anarres.lzo.LzoInputStream.fill(LzoInputStream.java:119)
at org.anarres.lzo.LzoInputStream.read(LzoInputStream.java:90)

So, while initializing LzoInputStream I'm wrong or after that I need to do something? Any suggestions will be appreciated!

Upvotes: 2

Views: 1494

Answers (2)

harshabangi
harshabangi

Reputation: 1

For a .lzo file format, you should first read the header information and then pass it on to the LzoInputStream. Then you can read the actual data till you reach eof.

I guess first 37 bytes are header related info and the compression algorithm information is available in 16th byte. LZO header format is documented at https://gist.github.com/jledet/1333896

Upvotes: 0

Ritesh K
Ritesh K

Reputation: 195

Please ensure that lzo stream are are flushed and closed during compression.

Upvotes: 0

Related Questions