TikTokExperience
TikTokExperience

Reputation: 25

Python zlib compression & decompression

I have a base45 decoded string that I successfully decompress with output = zlib.decompress(string), where string is:

b'x\xda\xbb\xd4\xe2\xbb\x88\xc5C\xe9l\xe7\xb6\xcd\x9dm\x9a\x8cj\x0b"\x19\xf9\x97\xb0H%]6y\xc0&\x95\xf0\x837\x9b1\xc91\xc4\x92\x91y!\xe3\x92\xc4\xb2\xc6UI)y\x8cI\xb9\x89\xb9\xfeA\xee\xba\x86\x06\x06\x06\xc6\x06F\x86\xa6Ie\x05\xe9^\x06\xe6N\x11\x06\xc6I)%YF@1]\x03s]#\xc3\xa4\xe4|\xa0\xee\xa4\xe4\xcc\n\xad\xd0 ?\xab\xd00gO+\x03C+\xc7\x10+\xd7\xa80\x13\xaf /\xe3p\xf7p\x93pw\x8b0O\xe3\xf0p3g\x13KSe\xcf\xa4\xdc\x82\x1c\xd7P}C}#\x03}CS#\x8b\xa4\xcc\xe2\ni\xdf\xcc\xbc\xcc\xe2\x92\xa2J\x85\xfc4\x05\x8f\xd4\xc4\x9c\x92\x0c\x1d\x05\xc7R\xa0HfbRq\nSRIz\xa6\x85\x89\x81\xa9\xb1\xa5\x81\x81Yr^b\xee\x92\xe4\xb4\xbc\x924\'\x1fWO\xd7\xa0\xa4\xb4\xbc4\xa7\x9c\xd4\xcc\xd4\xa2\xe4\xf4\xbc\x92,_\xc7\x08O_O\x1fOG\xbf\xa4\xf4\xbc,\xdf\xc4\x8a\xcc\xdc\xcc\x9c\xcc\xc4\xbc\xe4\xb2\xd4\xa2TC=c=\x83\xe4\x94\xfc$\xa0/\x0c\x80\xbe\xb0\xd454\x8ep\xe8K\xf7\xf1x\xb9Q\xfc\xc2\x93\x86\x8d\xc5\x99\xba7\xb4\x98\xe4\xe7d\xder2\xd4\xd457\xd8\xd2X3a\x8a\xa8\x91\xb4e_\xe9\x8e\x9ai\xfaj=V\x17\x9c"\xcaR\xb8Um\xf2\x1dN\xdf\x96\x93xzs\xf3\xa9\xbb\x00\x1c[z\x03x\xda\xbb\xd4\xe2\xbb\x88\xc5C\xe9l\xe7\xb6\xcd\x9dm\x9a\x8cj\x0b"\x19\xf9\x97\xb0H%]6y\xc0&\x95\xf0\x837\x9b1\xc91\xc4\x92\x91y!\xe3\x92\xc4\xb2\xc6UI)y\x8cI\xb9\x89\xb9\xfeA\xee\xba\x86\x06\x06\x06\xc6\x06F\x86\xa6Ie\x05\xe9^\x06\xe6N\x11\x06\xc6I)%YF@1]\x03s]#\xc3\xa4\xe4|\xa0\xee\xa4\xe4\xcc\n\xad\xd0 ?\xab\xd00gO+\x03C+\xc7\x10+\xd7\xa80\x13\xaf /\xe3p\xf7p\x93pw\x8b0O\xe3\xf0p3g\x13KSe\xcf\xa4\xdc\x82\x1c\xd7P}C}#\x03}CS#\x8b\xa4\xcc\xe2\ni\xdf\xcc\xbc\xcc\xe2\x92\xa2J\x85\xfc4\x05\x8f\xd4\xc4\x9c\x92\x0c\x1d\x05\xc7R\xa0HfbRq\nSRIz\xa6\x85\x89\x81\xa9\xb1\xa5\x81\x81Yr^b\xee\x92\xe4\xb4\xbc\x924\'\x1fWO\xd7\xa0\xa4\xb4\xbc4\xa7\x9c\xd4\xcc\xd4\xa2\xe4\xf4\xbc\x92,_\xc7\x08O_O\x1fOG\xbf\xa4\xf4\xbc,\xdf\xc4\x8a\xcc\xdc\xcc\x9c\xcc\xc4\xbc\xe4\xb2\xd4\xa2TC=c=\x83\xe4\x94\xfc$\xa0/\x0c\x80\xbe\xb0\xd454\x8ep\xe8K\xf7\xf1x\xb9Q\xfc\xc2\x93\x86\x8d\xc5\x99\xba7\xb4\x98\xe4\xe7d\xder2\xd4\xd457\xd8\xd2X3a\x8a\xa8\x91\xb4e_\xe9\x8e\x9ai\xfaj=V\x17\x9c"\xcaR\xb8Um\xf2\x1dN\xdf\x96\x93xzs\xf3\xa9\xbb\x00\x1c[z\x03'

Now I want to reverse the decompression with zlib.compress(output) to get the original string back. However the output is not the same:

b'x\x9c\xbb\xd4\xe2\xbb\x88\xc5C\xe9l\xe7\xb6\xcd\x9dm\x9a\x8cj\x0b"\x19\xf9\x97\xb0H%]6y\xc0&\x95\xf0\x837\x9b1\xc91\xc4\x92\x91y!\xe3\x92\xc4\xb2\xc6UI)y\x8cI\xb9\x89\xb9\xfeA\xee\xba\x86\x06\x06\x06\xc6\x06F\x86\xa6Ie\x05\xe9^\x06\xe6N\x11\x06\xc6I)%YF@1]\x03s]#\xc3\xa4\xe4|\xa0\xee\xa4\xe4\xcc\n\xad\xd0 ?\xab\xd00gO+\x03C+\xc7\x10+\xd7\xa80\x13\xaf /\xe3p\xf7p\x93pw\x8b0O\xe3\xf0p3g\x13KSe\xcf\xa4\xdc\x82\x1c\xd7P}C}#\x03}CS#\x8b\xa4\xcc\xe2\ni\xdf\xcc\xbc\xcc\xe2\x92\xa2J\x85\xfc4\x05\x8f\xd4\xc4\x9c\x92\x0c\x1d\x05\xc7R\xa0HfbRq\nSRIz\xa6\x85\x89\x81\xa9\xb1\xa5\x81\x81Yr^b\xee\x92\xe4\xb4\xbc\x924\'\x1fWO\xd7\xa0\xa4\xb4\xbc4\xa7\x9c\xd4\xcc\xd4\xa2\xe4\xf4\xbc\x92,_\xc7\x08O_O\x1fOG\xbf\xa4\xf4\xbc,\xdf\xc4\x8a\xcc\xdc\xcc\x9c\xcc\xc4\xbc\xe4\xb2\xd4\xa2TC=c=\x83\xe4\x94\xfc$\xa0/\x0c\x80\xbe\xb0\xd454\x8ep\xe8K\xf7\xf1x\xb9Q\xfc\xc2\x93\x86\x8d\xc5\x99\xba7\xb4\x98\xe4\xe7d\xder2\xd4\xd457\xd8\xd2X3a\x8a\xa8\x91\xb4e_\xe9\x8e\x9ai\xfaj=V\x17\x9c"\xcaR\xb8Um\xf2\x1dN\xdf\x96\x93xzs\xf3\xa9\xbb\x00\x1c[z\x03'

Here is my code:

zlibdata = base45.b45decode(data1)
cbordata = zlib.decompress(zlibdata)
print(zlibdata)
print("-----------------")
compressedData = zlib.compress(cbordata)
print(compressedData)

What am I doing wrong?

Upvotes: 0

Views: 1291

Answers (1)

Mark Adler
Mark Adler

Reputation: 112239

If you can decompress compressedData and get exactly cbordata back, then you are doing nothing wrong. The only thing wrong is your expectation or desire to get the same compressed data.

A lossless compressor guarantees that compression followed by decompression gives you exactly the same thing back. However there is no guarantee and no expectation that decompression followed by compression will give the same thing back.

The same compression code with different compression levels or different settings will give different output. A different version of the compression code with the same settings can give different output. Different compression libraries will give different output.

Upvotes: 4

Related Questions