0

I have the following YAML file:

data: !!binary |
  H4sIAIDb/U8C/1NW1E/KzNMvzuBKTc7IV8hIzcnJVyjPL8pJ4QIA6N+MVxsAAAA=

The value is ASCII string that represents the following gzipped text:

#!/bin/sh
echo hello world

In python 3, with yaml.safe_load() function, I loaded the file content python into dictionary object:

{'data': b'\x1f\x8b\x08\x00\x80\xdb\xfdO\x02\xffSV\xd4O\xca\xcc\xd3/\xce\xe0JM\xce\xc8W\xc8H\xcd\xc9\xc9W(\xcf/\xcaI\xe1\x02\x00\xe8\xdf\x8cW\x1b\x00\x00\x00'}

Next, I used zlib.decompress() function to decode the hex string to a simple string. However, I encountered the following error:

>>> text = Path("/home/admin/user.yaml").read_text()
>>> yaml_dict = yaml.safe_load(text)
>>> data=yaml_dict['data']
>>> zlib.decompress(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check

I expect to get the following output:

#!/bin/sh\necho hello world

To my understanding, \x1f\x8b\x08 is the header for gzipped files. So I don't understand why I still getting this error. Is there any way to work around this?

*Since I'm getting raw zipped strings (as bytes object) and do not read directly from a gzip file, I'm not sure if gzip package can help in my case..

Any help will be appreciated.

  • 1
    See comment in linked answer, Python requires `zlib.decompress(data, 15 + 32)`. `gzip.decompress(data)` also works. – flyx Feb 20 '22 at 16:56

0 Answers0