Java解压缩字节数组 [英] Java decompressing array of bytes

查看:217
本文介绍了Java解压缩字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器(C ++)上,使用 ZLib 函数压缩二进制数据:

On server (C++), binary data is compressed using ZLib function:

compress2()

并将其发送到客户端(Java)。
在客户端(Java),应使用以下代码片段解压缩数据:

and it's sent over to client (Java). On client side (Java), data should be decompressed using the following code snippet:

public static String unpack(byte[] packedBuffer) {
    InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream( packedBuffer);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    int readByte;
    try {
        while((readByte = inStream.read()) != -1) {
            outStream.write(readByte);
        }
    } catch(Exception e) {
        JMDCLog.logError(" unpacking buffer of size: " + packedBuffer.length);
        e.printStackTrace();
    // ... the rest of the code follows
}

问题是当它试图读入while循环时它总是抛出:

Problem is that when it tries to read in while loop it always throws:


java.util.zip.ZipException:存储的块长度无效

java.util.zip.ZipException: invalid stored block lengths

在我检查之前对于其他可能的原因可以有人请电话我可以使用compress2在一侧压缩,并使用上面的代码在另一侧解压缩,所以我可以消除这个问题吗?此外,如果有人可能知道这里可能出现的问题(我知道我没有在这里提供过多的代码,但项目相当大。

Before I check for other possible causes can someone please tell me can I compress on one side with compress2 and decompress it on the other side using above code, so I can eliminate this as a problem? Also if someone has a possible clue about what might be wrong here (I know I didn't provide too much of of the code in here but projects are rather big.

谢谢。

推荐答案

InflaterInputStream 期待原始deflate数据( RFC 1951 ),而 compress2()正在生成zlib包装的deflate数据( RFC 1950 。

InflaterInputStream is expecting raw deflate data (RFC 1951), whereas compress2() is producing zlib-wrapped deflate data (RFC 1950 around RFC 1951).

< a href =http://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html\"rel =nofollow> Inflater 会处理zlib包装的数据(除非你给它 nowrap 选项)。去图。

这篇关于Java解压缩字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆