2

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.

Community
  • 1
  • 1
dshapiro
  • 1,075
  • 14
  • 24

2 Answers2

2

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?

Community
  • 1
  • 1
John Machin
  • 81,303
  • 11
  • 141
  • 189
  • These were good ideas! These tests showed that the Python and Java I was using were equivalent compression algorithms and that the Java code could successfully uncompress some strings (just not the ones being sent from Python for some reason). We're deferring the functionality for now, but thanks for your help. – dshapiro Apr 06 '11 at 15:45
  • @UnicornZed: Please consider accepting the answer (big tick symbol to the left of the answer). – John Machin Apr 06 '11 at 20:12
0

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.

bvk256
  • 1,837
  • 3
  • 20
  • 38
  • I tried using the nowrap parameter to no avail. It was something I was unaware of, though, so thanks for bringing it to my attention. – dshapiro Apr 06 '11 at 15:46