在服务器(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).
Inflater 会处理zlib包装的数据(除非你给它 nowrap 选项)。去图。
更多推荐
Java解压缩字节数组
发布评论