Skip to content

Instantly share code, notes, and snippets.

@jdferrell3
Created December 17, 2018 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdferrell3/5e3c7037688c07efa82db6917ecb0eed to your computer and use it in GitHub Desktop.
Save jdferrell3/5e3c7037688c07efa82db6917ecb0eed to your computer and use it in GitHub Desktop.
gzip decompress string
import gzip
import base64
import StringIO
# python bgzip.py
# H4sIAErhF1wC/8tIzcnJBwCGphA2BQAAAA==
# hello
def gzip_and_base64(s):
out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode='w') as fh:
fh.write(s)
# return gzipped string base64 encoded
return base64.b64encode(out.getvalue())
def base64_and_gunzip(s):
decoded = base64.b64decode(s)
fo = StringIO.StringIO(decoded)
return gzip.GzipFile(fileobj=fo).read()
zipped = gzip_and_base64('hello')
print(zipped)
unzipped = base64_and_gunzip(zipped)
print(unzipped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment