Home > Enterprise >  Decompress a gzip compressed dictionary object using python
Decompress a gzip compressed dictionary object using python

Time:09-22

I need to decompress this "H4sIAAAAAAAA/6tWKkktLjFUsjI00lEAs42UrCAMpVoAbyLr R0AAAA=" which actually is compressed form of {"test1":12, "test2": "test"}. Now in python I'm using gzip library and getting below mentioned response:

>>> import gzip
>>> gzip.decompress("H4sIAAAAAAAA/6tWKkktLjFUsjI00lEAs42UrCAMpVoAbyLr R0AAAA=".encode("UTF-8"))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 548, in decompress
    return f.read()
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 292, in read
    return self._buffer.read(size)
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 479, in read
    if not self._read_gzip_header():
  File "/data/python-3.8.10/lib/python3.8/gzip.py", line 427, in _read_gzip_header
    raise BadGzipFile('Not a gzipped file (%r)' % magic)
gzip.BadGzipFile: Not a gzipped file (b'H4')

Is there any way to decompress the string in python ?

CodePudding user response:

The string is Base64 encoded. Therefore:-

import gzip
import base64

b = base64.b64decode('H4sIAAAAAAAA/6tWKkktLjFUsjI00lEAs42UrCAMpVoAbyLr R0AAAA=')
r = gzip.decompress(b)
print(r.decode())
  • Related