Reputation: 1995
I am trying to decompress a byte64 encoded string in Python 2.7.
I can verify that my string is valid by running this in the command line:
echo -n "MY_BASE64_ENCODED_STRING" | base64 -d | zcat
However, if I run this in Python2.7:
b64_data = 'MY_BASE64_ENCODED_STRING'
text_data = zlib.decompress(base64.b64decode(b64_data))
I get an exception:
Error -3 while decompressing data: incorrect header check
Should I pass extra parameters to zlib.decompress to make it work?
Upvotes: 5
Views: 4647
Reputation: 14794
As noted in the comments, your data is in gzip format and not just zlib compressed data. In Python 2.7, you can use GzipFile
with StringIO
to process the string:
>>> from gzip import GzipFile
>>> from StringIO import StringIO
>>> from base64 import b64decode
>>> data = 'H4sIAEm2algAAytJLS7hAgDGNbk7BQAAAA=='
>>> GzipFile(fileobj=StringIO(b64decode(data))).read()
'test\n'
Upvotes: 5