Naru Gawa
Naru Gawa

Reputation: 13

zlib gzip decompression python

my question is possible to extract a compressed files (gzip deflate) http compression with php or python or bash ?

i test this

import zlib

str_object1 = open('test.png', 'rb').read()
str_object2 = zlib.decompress(str_object1)
f = open('my_recovered_log_file', 'wb')
f.write(str_object2)
f.close()

and a get this result

File "testgz", line 4, in <module>
    str_object2 = zlib.decompress(str_object1)
zlib.error: Error -3 while decompressing data: incorrect header check

ty

Upvotes: 0

Views: 3422

Answers (2)

Mark Adler
Mark Adler

Reputation: 112239

The PNG file format has embedded in it one or more zlib streams. You would need to decode the format in order to find them, at which point you could use zlib.decompress to decompress them. You cannot use zlib to decompress a .png file from the start.

See the PNG specification for the format.

Your question seems confused, since you give an example of trying to decode a .png file, but you ask about "(gzip deflate) http compression", which is an entirely different thing. Yes, you can use zlib.decompress on those, with the proper usage of the wbits parameter.

Upvotes: 1

CrazyWearsPJs
CrazyWearsPJs

Reputation: 81

Although zlib uses the same mechanisms to compress/decompress as gzip, they use different headers as their checksums.

Python's zlib can uncompress gzipped files, but Python provides a convenience library gzip for this purpose.

Upvotes: 1

Related Questions