Created
December 17, 2018 18:13
-
-
Save jdferrell3/5e3c7037688c07efa82db6917ecb0eed to your computer and use it in GitHub Desktop.
gzip decompress string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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