import gzip
import shutil
with gzip.open('file.txt.gz', 'rb') as f_in:
    with open('file.txt', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)
Answer from Matt on Stack Overflow
🌐
Python
docs.python.org › 3 › library › gzip.html
gzip — Support for gzip files
The gzip module provides the GzipFile class, as well as the open(), compress() and decompress() convenience functions. The GzipFile class reads and writes gzip-format files, automatically compressing or decompressing the data so that it looks like an ordinary file object.
🌐
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.
Discussions

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
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
Python gzip: is there a way to decompress from a string? - Stack Overflow
You can use gzip.decompress from the gzip builtin Python library(available for Python 3.2+). More on stackoverflow.com
🌐 stackoverflow.com
lambda to split .gz file into ingestible chunks
Check out stack overflow- https://stackoverflow.com/questions/56139995/split-s3-file-into-smaller-files-of-1000-lines More on reddit.com
🌐 r/snowflake
6
1
October 20, 2023
🌐
Linux Hint
linuxhint.com › python-gzip-decompress-function
Python Gzip Decompress Function – Linux Hint
The “gzip.decompress()” function is a powerful tool for decompressing data that has been compressed via the “GZIP” algorithm. It is an essential function for any Python developer to work with compressed data files. This function takes a compressed data string as an argument and returns ...
🌐
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.
🌐
W3Schools
w3schools.com › python › ref_module_gzip.asp
Python gzip Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import gzip data = b"Emil" c = gzip.compress(data) print(len(c) > 0) print(gzip.decompress(c)) Try it Yourself »
🌐
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

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 first thing you can do is open up a file with compressed data, and then use the gzip.read() function on it. This will return the decompressed data exactly like it was before.
🌐
Delft Stack
delftstack.com › home › howto › python › python gzip decompress
How to Decompress Gzip in Python | Delft Stack
February 2, 2024 - The syntax of the gzip decompression method is shown below. ... Now, let’s use this function in our examples. First, we need to install the library using the following command. ... Once the Gzip library is installed, we can import it using the following line of code. ... Let’s start with an example, as shown below. # python import gzip value = b"This string is encoded in Python." value = gzip.compress(value) print("compressed value is: ", value) compressed = gzip.decompress(value) print("Decompressed value is: ", compressed)
🌐
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).
🌐
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
🌐
Stack Abuse
stackabuse.com › bytes › how-to-unzip-a-gz-file-using-python
How to Unzip a .gz File Using Python
August 14, 2023 - In this Byte, we'll explore a few ways to unzip a .gz file using Python. We'll get into Python's gzip module, read the contents of a .gz file, and more. .gz is the file extension for Gzip files (GNU zip), which is a file compression and decompression tool created by the GNU project.
🌐
MicroPython
docs.micropython.org › en › latest › library › gzip.html
gzip – gzip compression & decompression — MicroPython latest documentation
When the mode argument is "rb", reads from the GzipFile instance will decompress the data in the underlying stream and return decompressed data.
🌐
Medium
medium.com › @anirudhbishnoi5 › gzip-module-in-python-230aafd7d17a
gzip module in Python. gzip is an inbuilt module in python… | by Anirudh Bishnoi | Medium
April 9, 2023 - the gzip class would automatically read and write the gzip format files and automatically compresses and decompresses the files into file object.
🌐
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
🌐
Xah Lee
xahlee.info › python › gzip.html
Python: Compress Decompress Gzip File
March 23, 2019 - # compress a gzip file import gzip input = open("/Users/joe/xxtest", 'rb') s = input.read() input.close() output = gzip.GzipFile("/Users/joe/xxtest.gz", 'wb') output.write(s) output.close() print("done") # decompress a gzip file import gzip input = gzip.GzipFile("/Users/joe/xxtest.gz", 'rb') s = input.read() input.close() output = open("/Users/joe/xxtest", 'wb') output.write(s) output.close() print("done")
🌐
Stephen Brennan
brennan.io › 2020 › 09 › 22 › compression-curves
Visualizing gzip compression with Python! • Stephen Brennan
September 22, 2020 - In the above code, we save the open file object as compressed before giving it over to the GzipFile. That way, as we read the decompressed data out of gzip_file, we’ll be able to use the tell() method to see how far we are through the compressed file.