Skip to content

Instantly share code, notes, and snippets.

@carlos-jenkins
Created September 27, 2018 08:55
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 carlos-jenkins/5c7685e67285135bfaf66ac6e6967361 to your computer and use it in GitHub Desktop.
Save carlos-jenkins/5c7685e67285135bfaf66ac6e6967361 to your computer and use it in GitHub Desktop.
Compressing and Decompressing in Python 3
from zlib import compress, Z_BEST_COMPRESSION
# Compressing
def compress_ratio(original, compressed):
obytes = len(original)
cbytes = len(compressed)
return (obytes - cbytes) / obytes
original = b'Hello World'
compressed = compress(original, Z_BEST_COMPRESSION)
ratio = compress_ratio(original, compressed)
print('Compressed: {:.2f}%'.format(100.0 * ratio))
# Decompressing
from zlib import decompress
print(decompress(compressed))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment