Skip to content

Instantly share code, notes, and snippets.

@arwankhoiruddin
Created August 6, 2022 09:37
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 arwankhoiruddin/64ae619b387f0bc7aa944dc48a3dc6f4 to your computer and use it in GitHub Desktop.
Save arwankhoiruddin/64ae619b387f0bc7aa944dc48a3dc6f4 to your computer and use it in GitHub Desktop.
This code is from https://stackoverflow.com/questions/29243119/how-to-compress-or-compact-a-string-in-python, modified based on the comment to encode and decode string, not byte
import sys
import zlib
text="""This function is the primary interface to this module along with
decompress() function. This function returns byte object by compressing the data
given to it as parameter. The function has another parameter called level which
controls the extent of compression. It an integer between 0 to 9. Lowest value 0
stands for no compression and 9 stands for best compression. Higher the level of
compression, greater the length of compressed byte object."""
# Checking size of text
text_size=sys.getsizeof(text)
print("\nsize of original text",text_size)
# Compressing text
compressed = zlib.compress(text.encode())
# Checking size of text after compression
csize=sys.getsizeof(compressed)
print("\nsize of compressed text",csize)
# Decompressing text
decompressed=zlib.decompress(compressed).decode()
#Checking size of text after decompression
dsize=sys.getsizeof(decompressed)
print("\nsize of decompressed text",dsize)
print("\nDifference of size= ", text_size-csize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment