Java解压缩字节数组

编程入门 行业动态 更新时间:2024-10-28 07:27:56
本文介绍了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).

Inflater 会处理zlib包装的数据(除非你给它 nowrap 选项)。去图。

更多推荐

Java解压缩字节数组

本文发布于:2023-10-30 01:15:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   解压缩   字节   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!