gzip and zlib use slightly different headers.

See How can I decompress a gzip stream with zlib?

Try d = zlib.decompressobj(16+zlib.MAX_WBITS).

And you might try changing your chunk size to a power of 2 (say CHUNKSIZE=1024) for possible performance reasons.

Answer from wisty on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ gzip.html
gzip โ€” Support for gzip files
Changed in version 3.14: The mtime parameter now defaults to 0 for reproducible output. For the previous behaviour of using the current time, pass None to mtime. ... Decompress the data, returning a bytes object containing the uncompressed data.
Discussions

Faster decompression of gzip files
Pitch Decompressing gzip streams is an extremely common practice. Most web browsers support gzip decompression, as such most (virtually all) servers return gzip compressed data (when the gzip suppo... More on github.com
๐ŸŒ github.com
5
August 1, 2022
Decompression of GZIP Compressed String
1f8b is gzip as already mentioned. Extraction with gzip works fine. You falsely assume that you can push some encoding on binary data. Checking it out in a hex editor it seems the file is just an array of plain ints, mostly ascending (though not always) until there is a cut in the middle of the file and it repeats exactly the same values. from binascii import hexlify, unhexlify import gzip from struct import pack, unpack payload = unhexlify("1F8B080000000000000AEDD2CF4F0F701CC7F1A77C554828BE7DA9A4A25F8AEF37947E51E8DB56072E6CB994A5AD1CFC1807ADAD360EB1E1C08C038772D096696CD5212B0E6C3860B41933077360B30E75505BE679F8FE0D4E1D1EC7CFFBF3FEF10A00A92A51838EAB5BB734AC57FAAE05A52D81B01AD5AE3E8DE8B72271D0AB4FAA590A0F95178021552E83B76A8B873FBA9A00D989BE55E372F8AA332B206125DC5538095EA87995B575311982ABADA5DA35F0519D6BED493752FCC701C675689DBDEAC27A480A42BFCAD2E0B55A4230A3CB1B2063233C567D3A7CD6A90C88CB84DB2ADE04CF74240B7EAA6733A464C30355E5C03B9DC885395DDB02395B61544D79F04DE7F321B100EE2952082F75AC08A675691B848ADD8DEA5CF8944E6E87BFBAB903F2C3F0548723F0435DA590BC130654BE0BDEA87537CCEA4A196496C31345F7C0179DAE804025DC5149153CD7D16AF8A5DE1AEFBC170655BD0FDEABBD16E675BD0E72F7C3989A0E3887CE1D84F87A6B2914F5767AA4098DE8BE8635A90F9A56D00045D5157336A6436D6A55738CB158CCDD62EEF8DFB9FB07D858DEE0F0040000") data = gzip.decompress(payload) values = unpack(str(len(data)//4)+"i",data) v1,v2 = values[:len(values)//2], values[len(values)//2:] assert v1 == v2 # Same data stored twice? More on reddit.com
๐ŸŒ r/learnpython
2
1
July 16, 2023
python - How to uncompress gzipped data in a byte array? - Stack Overflow
To decompress a gzip stream, either use the gzip module, or use the zlib module with arcane arguments derived from much googling or reading the C-library docs at zlib.net. More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 22, 2018
Introducing zune-inflate: The fastest Rust implementation of gzip/Zlib/DEFLATE
Is streaming support planned? Also, is it possible to decompress into a user provided buffer -- even if this buffer has to be initialized? More on reddit.com
๐ŸŒ r/rust
30
214
February 13, 2023
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ gzip-stream
gzip-stream ยท PyPI
# aiobotocore example import aiobotocore from gzip_stream import AsyncGZIPDecompressedStream AWS_ACCESS_KEY_ID = "KEY_ID" AWS_SECRET_ACCESS_KEY = "ACCESS_KEY" BUCKET = "AWESOME_BUCKET" upload_client = MyAsyncUploadClient() session = aiobotocore.get_session() async with session.create_client( service_name="s3", endpoint_url="s3_endpoint", aws_secret_access_key=AWS_SECRET_ACCESS_KEY, aws_access_key_id=AWS_ACCESS_KEY_ID, ) as client: response = await client.get_object(Bucket=BUCKET, Key='my_very_big_1tb_file.txt.gz') async for decompressed_chunk in GzipAsyncReaderWrapper(response["Body"])): await upload_client.upload_fileobj(decompressed_chunk) Module works on Python ~= 3.5. ... Public Domain: CC0 1.0 Universal. ... Download the file for your platform. If you're not sure which to choose, learn more about installing packages. ... Details for the file gzip-stream-1.2.0.tar.gz.
      ยป pip install gzip-stream
    
Published ย  Dec 08, 2021
Version ย  1.2.0
๐ŸŒ
GitHub
github.com โ€บ leenr โ€บ gzip-stream
GitHub - leenr/gzip-stream: Streaming GZIP compression for Python ยท GitHub
# aiobotocore example import aiobotocore from gzip_stream import AsyncGZIPDecompressedStream AWS_ACCESS_KEY_ID = "KEY_ID" AWS_SECRET_ACCESS_KEY = "ACCESS_KEY" BUCKET = "AWESOME_BUCKET" upload_client = MyAsyncUploadClient() session = aiobotocore.get_session() async with session.create_client( service_name="s3", endpoint_url="s3_endpoint", aws_secret_access_key=AWS_SECRET_ACCESS_KEY, aws_access_key_id=AWS_ACCESS_KEY_ID, ) as client: response = await client.get_object(Bucket=BUCKET, Key='my_very_big_1tb_file.txt.gz') async for decompressed_chunk in AsyncGZIPDecompressedStream(response["Body"])): await upload_client.upload_fileobj(decompressed_chunk) Module works on Python ~= 3.5.
Starred by 32 users
Forked by 5 users
Languages ย  Python
๐ŸŒ
Michaelehead
michaelehead.com โ€บ 2019 โ€บ 12 โ€บ 28 โ€บ python-streaming-unzip.html
Unzipping a large gzip file in Python | Michael Head
December 28, 2019 - To showcase the power of this method for decompressing a large file without exhausting memory or disk space, I put together a repository that demonstrates the various scenarios I tried: https://github.com/headquarters/python-streaming-unzip. Here you can play around with a Docker container that can execute a Python script that attempts to decompress a gzipped file into memory, onto disk, or via the streaming method.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ gzip-decompresss-in-python
gzip.decompress(s) in Python - GeeksforGeeks
March 26, 2020 - 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.
๐ŸŒ
MicroPython
docs.micropython.org โ€บ en โ€บ latest โ€บ library โ€บ gzip.html
gzip โ€“ gzip compression & decompression โ€” MicroPython latest documentation
This module allows compression and decompression of binary data with the DEFLATE algorithm used by the gzip file format. ... Prefer to use deflate.DeflateIO instead of the functions in this module as it provides a streaming interface to compression and decompression which is convenient and ...
๐ŸŒ
GitHub
gist.github.com โ€บ jiankaiwang โ€บ f5056a34bbbf595c9553efb36aff64ec
Compress and decompress a gzip file in python. ยท GitHub
Compress and decompress a gzip file in python. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
๐ŸŒ
CodersLegacy
coderslegacy.com โ€บ home โ€บ python โ€บ python gzip module โ€“ compress and decompress files
Python gzip module - compress and decompress files - CodersLegacy
January 30, 2022 - The solution to this is compressing files with the help of a compression library to produce smaller file sizes. In Python, we can make use of the gzip library, used to compress and decompress files. Serialization is the process of converting an object to a byte stream, and the inverse of which is converting a byte stream back to a python object.
๐ŸŒ
Rational Pie
rationalpie.wordpress.com โ€บ 2010 โ€บ 06 โ€บ 02 โ€บ python-streaming-gzip-decompression
Python Streaming gzip Decompression | Rational Pie
June 1, 2010 - The gzip module in python works with files. It does not have an interface for decompressing a stream of data. This however becomes a problem if you want to work with files that are difficult (or impossible!) to read in memory at once.
๐ŸŒ
ProgramCreek
programcreek.com โ€บ python โ€บ example โ€บ 93867 โ€บ gzip.decompress
Python Examples of gzip.decompress
def getstream(file): # TODO -- gcs if file.startswith('http://') or file.startswith('https://'): response = requests.get(file) status_code = response.status_code #TODO Do something with this if file.endswith('.gz'): content = response.content text = gzip.decompress(content).decode('utf-8') else: text = response.text f = io.StringIO(text) return text elif file.endswith('.gz'): f = gzip.open(file, mode='rt') else: f = open(file, encoding='UTF-8') return f
๐ŸŒ
Quora
quora.com โ€บ Python-How-can-I-decompress-a-string-compressed-by-GZipStream-in-NET
Python: How can I decompress a string compressed by GZipStream in .NET? - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
๐ŸŒ
GitHub
github.com โ€บ python โ€บ cpython โ€บ issues โ€บ 95534
Faster decompression of gzip files ยท Issue #95534 ยท python/cpython
August 1, 2022 - When reading individual lines from a file (quite common) this actually queries a io.BufferedReader instance which reads from the _GzipReader in io.DEFAULT_BUFFER_SIZE chunks. This means only 8KB requested. But still, 8KB is read from the _PaddedFile object. With typical compression rates this means anywhere between 4KB to 7KB of unconsumed tail will be returned. This creates a new object. Meaning allocating memory again. The same data is reread in the next iteration. This means in case of a 70% decompression rate, the same data is memcpy'd around 2 to 3 times.
Author ย  python
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ decompression of gzip compressed string
r/learnpython on Reddit: Decompression of GZIP Compressed String
July 16, 2023 -

Hi all! In order to get graph data out of our machines, I was looking to get into the data. The data seemed to be in the database, stored in the deprecated IMAGE format. An example string looked like this:

0x1F8B080000000000000AEDD2CF4F0F701CC7F1A77C554828BE7DA9A4A25F8AEF37947E51E8DB56072E6CB994A5AD1CFC1807ADAD360EB1E1C08C038772D096696CD5212B0E6C3860B41933077360B30E75505BE679F8FE0D4E1D1EC7CFFBF3FEF10A00A92A51838EAB5BB734AC57FAAE05A52D81B01AD5AE3E8DE8B72271D0AB4FAA590A0F95178021552E83B76A8B873FBA9A00D989BE55E372F8AA332B206125DC5538095EA87995B575311982ABADA5DA35F0519D6BED493752FCC701C675689DBDEAC27A480A42BFCAD2E0B55A4230A3CB1B2063233C567D3A7CD6A90C88CB84DB2ADE04CF74240B7EAA6733A464C30355E5C03B9DC885395DDB02395B61544D79F04DE7F321B100EE2952082F75AC08A675691B848ADD8DEA5CF8944E6E87BFBAB903F2C3F0548723F0435DA590BC130654BE0BDEA87537CCEA4A196496C31345F7C0179DAE804025DC5149153CD7D16AF8A5DE1AEFBC170655BD0FDEABBD16E675BD0E72F7C3989A0E3887CE1D84F87A6B2914F5767AA4098DE8BE8635A90F9A56D00045D5157336A6436D6A55738CB158CCDD62EEF8DFB9FB07D858DEE0F0040000

By googling (and ChatGPT ofc) it turned out it looks like a GZIP compressed string, given the format start (1F8B080, GZIP format spec RFC 1952). Howerver, I can't figure to get this string decompressed using Python.

Code:

import gzip

import binascii import zlib import bz2 import lzma

The input hexadecimal string

input_hex_string = "0x1F8B080000000000000AEDD2CF4F0F701CC7F1A77C554828BE7DA9A4A25F8AEF37947E51E8DB56072E6CB994A5AD1CFC1807ADAD360EB1E1C08C038772D096696CD5212B0E6C3860B41933077360B30E75505BE679F8FE0D4E1D1EC7CFFBF3FEF10A00A92A51838EAB5BB734AC57FAAE05A52D81B01AD5AE3E8DE8B72271D0AB4FAA590A0F95178021552E83B76A8B873FBA9A00D989BE55E372F8AA332B206125DC5538095EA87995B575311982ABADA5DA35F0519D6BED493752FCC701C675689DBDEAC27A480A42BFCAD2E0B55A4230A3CB1B2063233C567D3A7CD6A90C88CB84DB2ADE04CF74240B7EAA6733A464C30355E5C03B9DC885395DDB02395B61544D79F04DE7F321B100EE2952082F75AC08A675691B848ADD8DEA5CF8944E6E87BFBAB903F2C3F0548723F0435DA590BC130654BE0BDEA87537CCEA4A196496C31345F7C0179DAE804025DC5149153CD7D16AF8A5DE1AEFBC170655BD0FDEABBD16E675BD0E72F7C3989A0E3887CE1D84F87A6B2914F5767AA4098DE8BE8635A90F9A56D00045D5157336A6436D6A55738CB158CCDD62EEF8DFB9FB07D858DEE0F0040000"

Remove the "0x" prefix and convert the hexadecimal string to bytes

compressed_data = binascii.unhexlify(input_hex_string[2:])

List of character encodings to try

encodings_to_try = ["utf-8", "latin-1", "ascii", "utf-16", "utf-32", "cp1252", "cp850"]

List of decompression methods to try

decompression_methods = [ ("gzip", gzip.decompress), ("zlib", zlib.decompress), ("bz2", bz2.decompress), ("lzma", lzma.decompress), ]

for decompression_name, decompression_func in decompression_methods: print(f"Trying decompression with {decompression_name}:") for encoding in encodings_to_try: try: decompressed_data = decompression_func(compressed_data) print(f"Decompressed and decoded with {encoding}:") print(decompressed_data.decode(encoding)) except Exception as e: print(f"Decompression with {decompression_name} and {encoding} failed: {e}") print()

Does someone of you have experience on this type of compression/ can someone get useful data out of this string? Thanks in advance

๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python gzip module
Python gzip Module| Scaler Topics
December 15, 2022 - Intro: Used to decompress the data returns the bytes of the decompressed data. This Python gzip.decompress() function is capable of decompressing the multi-member gzip data (that is, multiple gzip blocks concatenated together).
๐ŸŒ
Openmv
docs.openmv.io โ€บ micropython โ€บ micropython libraries โ€บ gzip โ€“ gzip compression & decompression
gzip โ€“ gzip compression & decompression - MicroPython 1.28 documentation
This module allows compression and decompression of binary data with the DEFLATE algorithm used by the gzip file format. It provides one-shot compress()/decompress() helpers and a streaming GzipFile wrapper around an underlying file or stream object.
๐ŸŒ
GitHub
gist.github.com โ€บ davidcrawford โ€บ 6774493
Python UnzipStream โ€” A file-like wrapper around a gzipped stream that uncompresses as it reads.
February 1, 2018 - Python UnzipStream โ€” A file-like wrapper around a gzipped stream that uncompresses as it reads. - unzip.py
Top answer
1 of 2
43

zlib.decompress(data, 15 + 32) should autodetect whether you have gzip data or zlib data.

zlib.decompress(data, 15 + 16) should work if gzip and barf if zlib.

Here it is with Python 2.7.1, creating a little gz file, reading it back, and decompressing it:

>>> import gzip, zlib
>>> f = gzip.open('foo.gz', 'wb')
>>> f.write(b"hello world")
11
>>> f.close()
>>> c = open('foo.gz', 'rb').read()
>>> c
'\x1f\x8b\x08\x08\x14\xf4\xdcM\x02\xfffoo\x00\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x85\x11J\r\x0b\x00\x00\x00'
>>> ba = bytearray(c)
>>> ba
bytearray(b'\x1f\x8b\x08\x08\x14\xf4\xdcM\x02\xfffoo\x00\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x85\x11J\r\x0b\x00\x00\x00')
>>> zlib.decompress(ba, 15+32)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be string or read-only buffer, not bytearray
>>> zlib.decompress(bytes(ba), 15+32)
'hello world'
>>>

Python 3.x usage would be very similar.

Update based on comment that you are running Python 2.2.1.

Sigh. That's not even the last release of Python 2.2. Anyway, continuing with the foo.gz file created as above:

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> strobj = open('foo.gz', 'rb').read()
>>> strobj
'\x1f\x8b\x08\x08\x14\xf4\xdcM\x02\xfffoo\x00\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x85\x11J\r\x0b\x00\x00\x00'
>>> import zlib
>>> zlib.decompress(strobj, 15+32)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
zlib.error: Error -2 while preparing to decompress data
>>> zlib.decompress(strobj, 15+16)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
zlib.error: Error -2 while preparing to decompress data

# OK, we can't use the back door method. Plan B: use the 
# documented approach i.e. gzip.GzipFile with a file-like object.

>>> import gzip, cStringIO
>>> fileobj = cStringIO.StringIO(strobj)
>>> gzf = gzip.GzipFile('dummy-name', 'rb', 9, fileobj)
>>> gzf.read()
'hello world'

# Success. Now let's assume you have an array.array object-- which requires
# premeditation; they aren't created accidentally!
# The following code assumes subtype 'B' but should work for any subtype.

>>> import array, sys
>>> aaB = array.array('B')
>>> aaB.fromfile(open('foo.gz', 'rb'), sys.maxint)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
EOFError: not enough items in file
#### Don't panic, just read the fine manual
>>> aaB
array('B', [31, 139, 8, 8, 20, 244, 220, 77, 2, 255, 102, 111, 111, 0, 203, 72, 205, 201, 201, 87, 40, 207, 47, 202, 73, 1, 0, 133, 17, 74, 13, 11, 0, 0, 0])
>>> strobj2 = aaB.tostring()
>>> strobj2 == strobj
1 #### means True 
# You can make a str object and use that as above.

# ... or you can plug it directly into StringIO:
>>> gzip.GzipFile('dummy-name', 'rb', 9, cStringIO.StringIO(aaB)).read()
'hello world'
2 of 2
6

Apparently you can do this

import zlib
# ...
ungziped_str = zlib.decompressobj().decompress('x\x9c' + gziped_str)

Or this:

zlib.decompress( data ) # equivalent to gzdecompress()

For more info, look here: Python docs

๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ gzip
gzip โ€“ Read and write GNU zip files - Python Module of the Week
Now available for Python 3! Buy the book! ... The gzip module provides a file-like interface to GNU zip files, using zlib to compress and uncompress the data.