Skip to content Skip to sidebar Skip to footer

How Do You Compress A String, And Get A String Back Using Zlib?

I am trying to utilize Zlib for text compression. For example I have a string T='blah blah blah blah' I need to compress it for this string. I am using S=zlib.compress(T) to compre

Solution 1:

Program 1:

T = 'blah blah blah blah'
S = zlib.compress(T)
withopen("temp.zlib", "wb") as myfile:
    myfile.write(S)

This saves the compressed string in a file called temp.zlib so that program 2 can later retrieve and decompress it.

Program 2:

withopen("temp.zlib", "rb") as myfile:
    S = myfile.read()
T = zlib.decompress(S)

Post a Comment for "How Do You Compress A String, And Get A String Back Using Zlib?"