在 Java 中解压缩 zlib 压缩的字符串

发布于 2024-10-30 00:37:30 字数 713 浏览 2 评论 0原文

我有一个 Java 模块,它从远程 Python 脚本接收压缩字符串。 Python 脚本使用 zlib.compress() 压缩字符串。我只是想用 Java 解压缩它并将其显示给用户。

手册页 Java 的内置 zip.Deflater 对象非常明确地描述了如何解压缩使用 zlib.compress() 压缩的内容。然而,这个方法对我来说不起作用。根据我使用的编码,我要么收到“错误的标头检查”错误,要么解压缩返回空字符串。

那么,我该如何解压缩呢?数据在传输过程中不会被损坏,并且压缩字符串以“x\x9c”开头,这显然适合 zlib 压缩的内容。

我以前从未处理过这个级别的压缩/解压缩,并且感到很困惑。对于额外的加分,我希望能对压缩/未压缩和膨胀/放气之间进行解释。根据这个他们是不同的,但大多数互联网似乎将它们互换用于 zlib。这只会让寻找解决方案变得更加困难,因为我无法告诉你我是否真的在尝试“解压缩”或“膨胀”这些数据。

I have a Java module that is receiving a compressed string from a remote Python script. The Python script compresses the string using zlib.compress(). I simply want to uncompress it in Java and display it to the user.

The man page for Java's built-in zip.Deflater object describes pretty explicitly how to uncompress something that has been compressed using zlib.compress(). However, this method does not work for me. Depending on which encoding I use, I either get "Incorrect Header Check" errors or the uncompression returns an empty string.

So, how am I supposed to uncompress this? The data are not getting corrupted in transmission, and the compressed string begins with "x\x9c", which is apparently appropriate for zlib-compressed stuff.

I've never dealt with compression/uncompression on this level before and am getting confused. For extra credit, I'd appreciate an explanation between compressed/uncompressed and inflated/deflated. According to this they are different, but most of the internet seems to use them interchangeably for zlib. This just makes trying to find a solution even more difficult, as I couldn't tell you whether I'm actually trying to "uncompress" or "inflate" these data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-06 00:37:30

混乱的出现是因为一些聪明的火花开始将 zlib 协议描述为“deflate”。它可能会帮助您阅读 这些 Java 文档

另外这个主题也非常相关。

我建议您在 Python (A) 中执行

print repr(zlib.compress("The Quick Brown Dog etc etc")

,并使用 Deflater 使用等效 Java 代码来比较结果还要确保您可以 Inflate B 来恢复您的测试输入,检查您是否尝试过使用 Python 或 Java

进行“deflate” 。根据 @patthoyts 在您引用的 SO 主题中的回答?

The confusion has arisen because some bright spark started describing the zlib protocol as "deflate". It might help you to read the RFCs mentioned in these Java docs.

Also this SO topic is quite relevant.

I suggest that you do

print repr(zlib.compress("The quick brown dog etc etc")

in Python (A) and compare the result from using the equivalent Java code using Deflater (B). Also ensure that you can Inflate B to recover your test input. Check that you are not suffering from unicode <-> bytes complications in Python or Java or both.

Have you tried doing a Python "deflate" as per the answer by @patthoyts in the SO topic that you quoted?

°如果伤别离去 2024-11-06 00:37:30

看来Python的zlib.compress()使用了gzip,您确定使用nowrap参数创建Inflater以进行gzip兼容的解压缩吗?

我相信,膨胀/放气仅用于 DEFLATE 算法,而压缩/解压缩是更通用的术语。

It seems Python's zlib.compress() uses gzip, are you sure to create Inflater with nowrap parameter for gzip compatible uncompression?

Inflate/deflate is used only regarding DEFLATE algorithm I believe, whereas compress/uncompress is more general term.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文