Zlib.error: error – 3 while decompressing: incorrect header check in Python

As shown in the following code:

b64_encoded_bytes = base64.b64encode(zlib.compress(b'abcde'))
encoded_bytes_representation = str(b64_encoded_bytes)  # this the cause
zlib.decompress(base64.b64decode(encoded_bytes_representation))

Zlib.decopress error

zlib.error: Error -3 while decompressing: incorrect header check

The reason for this is the use of the str() method
The description of the str() method in the python3 documentation.
If neither encoding nor errors is given, str(object) returns object.str(), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a str() method, then str() falls back to returning repr(object).
The str() method only serves to put the

b'eJxLTEpOSQUABcgB8A=='

Converted to

"b'eJxLTEpOSQUABcgB8A=='"

The correct conversion should be to

"eJxLTEpOSQUABcgB8A=="

The solution is to specify the encoding method in the str() method

str(b64_encoded_bytes, 'utf-8')

Read More: