Created
September 27, 2018 08:55
-
-
Save carlos-jenkins/5c7685e67285135bfaf66ac6e6967361 to your computer and use it in GitHub Desktop.
Compressing and Decompressing in Python 3
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
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