本文介绍了如何使用 java-lzo 库解压缩 lzo 字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 java-lzo 库解压缩压缩字节数组.我正在关注 此参考资料.

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

我在 pom.xml 下面添加了 ma​​ven 依赖 -

I added below maven dependency to pom.xml -

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

我创建了一个方法,它接受 lzo 压缩字节数组和目标字节数组长度作为参数.

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

程序:

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();
}

我一度陷入困境,因为 stream.read() 总是返回-1".我检查了输入数组,它充满了数据.此外,我使用 stream.available() 方法进行了检查,但在我的情况下,此方法也始终返回0".但是如果我像 input.available() 这样检查 InputStream ,那么返回值是数组的长度.

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.

错误就像我说的返回-1"一样 -

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)

所以,在初始化 LzoInputStream 时我错了,或者之后我需要做些什么?任何建议将不胜感激!

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

推荐答案

对于 .lzo 文件格式,您应该首先读取头信息,然后将其传递给 LzoInputStream.然后就可以读取实际数据,直到达到eof.

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.

我猜前 37 个字节是与标头相关的信息,压缩算法信息在第 16 个字节中可用.LZO 标头格式记录在 https://gist.github.com/jledet/1333896

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

这篇关于如何使用 java-lzo 库解压缩 lzo 字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 06:47