Python gzip: is there a way to decompress from a string?

gzip.open is a shorthand for opening a file, what you want is gzip.GzipFile which you can pass a fileobj

open(filename, mode='rb', compresslevel=9)
    #Shorthand for GzipFile(filename, mode, compresslevel).

vs

class GzipFile
   __init__(self, filename=None, mode=None, compresslevel=9, fileobj=None)
   #    At least one of fileobj and filename must be given a non-trivial value.

so this should work for you

gzip_file_handle = gzip.GzipFile(fileobj=url_file_handle)

If your data is already in a string, try zlib, which claims to be fully gzip compatible:

import zlib
decompressed_data = zlib.decompress(gz_data, 16+zlib.MAX_WBITS)

Read more: http://docs.python.org/library/zlib.html‎

Tags:

Python

Gzip