0

I'm trying to inflate a zlib compressed file using Python with this code:

import zlib

data = open("3B42.110531.21.6A.HDF.Z", 'rb').read()
inflated = zlib.decompress(data)
f = open('3B42.110531.21.6A.HDF', 'wb')
f.write(inflated)
f.close()

I've already done several attempts with different options:

  • Adding a second parameter to zlib.decompress (zlib.decompress(data,-15))
  • Skipping the first two bytes zlib.decompress(data[2:-4]) / zlib.decompress(data[2:] /.. )
  • Basecoding to 64 bits.

Anyway, I keep failing with this message:

    Traceback (most recent call last):
  File "C:\opt\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\Tesis\data\uncompress.py", line 6, in <module>
    inflated = zlib.decompress(data)
error: Error -3 while decompressing data: incorrect header check

The only difference is using a negative parameter in zlib.decompress: invalid block type.

import zlib

data = open("3B42.110531.21.6A.HDF.Z", 'rb').read()
inflated = zlib.decompress(data,-15)
f = open('3B42.110531.21.6A.HDF', 'wb')
f.write(inflated)
f.close()


Traceback (most recent call last):
  File "C:\opt\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\Tesis\data\uncompress.py", line 6, in <module>
    inflated = zlib.decompress(data,-15)
error: Error -3 while decompressing data: invalid block type

I'm sure that the file is not corrupted, I can open it from WinRAR. (environment: Windows x64, Python 2.5, I guess that the file is in a Unix machine..binary downloaded)

I've already read the following links

Community
  • 1
  • 1
jpaulini
  • 1
  • 1
  • 1

2 Answers2

2

.Z indicates a LZC/compress file. Despite the name similarity, this compression format differs from gzip, which is what zlib implements.

Try using the command-line compress utility to uncompress the file (Your gzip program may also be able to decompress it).

phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    Just to add some clarification: zlib (the library) implements DEFLATE (the data format) as defined in RFC 1951, *and* gzip (RFC 1952), which is a wrapper around DEFLATE. There's also zlib the data format (RFC 1950), which is also a DEFLATE container. zlib the library *also* implements zlib the data format. Confused yet? ;-) – Cameron Jul 28 '11 at 19:18
1

The file extension '.Z' and the attempts you tried so far sound like you either use zLib wrong (but it seems correct according to your posted links) or the zLib stream isn't right at the beginning of the file.

You can use my tool Precomp with the file to detect the position of zLib stream(s) inside the file:

precomp -v -slow 3B42.110531.21.6A.HDF.Z

It should output something like this:

Possible zLib-Stream (slow mode) found at position 85, windowbits = 15
Can be decompressed to 9264 bytes

This will tell you both the position of the stream and the windowbits parameter to use (negated).

It will also tell you if there are zLib streams inside the file at all because as phihag said, it's possible that the file is compressed with something different than deflate/zLib. Note that in this case, there'll probably be some misdetections as the zLib header is only 2 bytes in size, but those can be identified by decompressing to <100 bytes.

Community
  • 1
  • 1
schnaader
  • 49,103
  • 10
  • 104
  • 136
  • Possible zLib-Stream (slow mode) found at position 4524, windowbits = 13 Can be decompressed to 3 bytes Less than 32 bytes, skipping. Possible zLib-Stream (slow mode) found at position 11760, windowbits = 12 Can be decompressed to 9 bytes Less than 32 bytes, skipping. Possible zLib-Stream (slow mode) found at position 21507, windowbits = 9 – jpaulini Jul 28 '11 at 20:07
  • Sorry, thanks in advance for your help.. I downloaded precomp, but when I see windowbits, It varies from 9, 13, 8, 12 and 15... I don´t have a clue really of what it means... (log is over this comment, I accidentally pressed Enter before ending the comment..) – jpaulini Jul 28 '11 at 20:10
  • This looks like the misdetection I mentioned, so this file indeed has no deflate/zLib streams in it and can't be decompressed using zLib. You should accept phihag's answer and look for a tool/library to decompress LZC/compress. – schnaader Jul 28 '11 at 20:43