Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Demo of how to gzip and gunzip a string in Python 3
import gzip
import io
def gzip_str(string_):
out = io.BytesIO()
with gzip.GzipFile(fileobj=out, mode='w') as fo:
bytes_obj = out.getvalue()
return bytes_obj
def gunzip_bytes_obj(bytes_obj):
in_ = io.BytesIO()
with gzip.GzipFile(fileobj=in_, mode='rb') as fo:
gunzipped_bytes_obj =
return gunzipped_bytes_obj.decode()
string_ = 'hello there!'
gzipped_bytes = gzip_str(string_)
original_string = gunzip_bytes_obj(gzipped_bytes)
assert string_ == original_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.