Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do I decode GZIP encoding?

I downloaded a webpage in my python script. In most cases, this works fine.

However, this one had a response header: GZIP encoding, and when I tried to print the source code of this web page, it had all symbols in my putty.

How do decode this to regular text?

like image 287
TIMEX Avatar asked Apr 22 '10 23:04

TIMEX


People also ask

How do I open a gzip file in Python?

To open a compressed file in text mode, use open() (or wrap your GzipFile with an io. TextIOWrapper ). The compresslevel argument is an integer from 0 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression.

What encoding does gzip use?

gzip is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding.

How do you unzip a string in Python?

With the help of gzip. decompress(s) method, we can decompress the compressed bytes of string into original string by using gzip. decompress(s) method. Return : Return decompressed string.


2 Answers

I use zlib to decompress gzipped content from web.

import zlib import urllib  f=urllib.request.urlopen(url)  decompressed_data=zlib.decompress(f.read(), 16+zlib.MAX_WBITS) 
like image 174
YOU Avatar answered Sep 24 '22 10:09

YOU


Decompress your byte stream using the built-in gzip module.

If you have any problems, do show the exact minimal code that you used, the exact error message and traceback, together with the result of print repr(your_byte_stream[:100])

Further information

1. For an explanation of the gzip/zlib/deflate confusion, read the "Other uses" section of this Wikipedia article.

2. It can be easier to use the zlib module than the gzip module if you have a string rather than a file. Unfortunately the Python docs are incomplete/wrong:

zlib.decompress(string[, wbits[, bufsize]])

...The absolute value of wbits is the base two logarithm of the size of the history buffer (the “window size”) used when compressing data. Its absolute value should be between 8 and 15 for the most recent versions of the zlib library, larger values resulting in better compression at the expense of greater memory usage. The default value is 15. When wbits is negative, the standard gzip header is suppressed; this is an undocumented feature of the zlib library, used for compatibility with unzip‘s compression file format.

Firstly, 8 <= log2_window_size <= 15, with the meaning given above. Then what should be a separate arg is kludged on top:

arg == log2_window_size means assume string is in zlib format (RFC 1950; what the HTTP 1.1 RFC 2616 confusingly calls "deflate").

arg == -log2_window_size means assume string is in deflate format (RFC 1951; what people who didn't read the HTTP 1.1 RFC carefully actually implemented)

arg == 16 + log_2_window_size means assume string is in gzip format (RFC 1952). So you can use 31.

The above information is documented in the zlib C library manual ... Ctrl-F search for windowBits.

like image 20
John Machin Avatar answered Sep 21 '22 10:09

John Machin