Skip to content

Instantly share code, notes, and snippets.

@prostoalex
Forked from temoto/helpers_data.py
Created February 2, 2014 06:35
Show Gist options
  • Select an option

  • Save prostoalex/8763862 to your computer and use it in GitHub Desktop.

Select an option

Save prostoalex/8763862 to your computer and use it in GitHub Desktop.
def gzip_string(s, level=6):
"""Compress string using gzip.
Default compression level is 6.
"""
zbuf = StringIO()
zfile = GzipFile(mode='wb', compresslevel=level, fileobj=zbuf)
zfile.write(s)
zfile.close()
return zbuf.getvalue()
def gunzip_string(s):
"""Decompress string using gzip.
See http://stackoverflow.com/questions/2695152/in-python-how-do-i-decode-gzip-encoding/2695466#2695466
"""
return zlib.decompress(s, 16 + zlib.MAX_WBITS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment