decompressing a gzip file using uzlib

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

decompressing a gzip file using uzlib

Post by spierepf » Mon Mar 30, 2020 12:47 pm

I am trying to decompress a gzip byte array using uzlib.decompress.

I started with

Code: Select all

$ cat /dev/null | gzip -f | hexdump -C
00000000  1f 8b 08 00 da e8 81 5e  00 03 03 00 00 00 00 00  |.......^........|
00000010  00 00 00 00                                       |....|
00000014
and attempted the following in micropython

Code: Select all

$ micropython
MicroPython v1.11 on 2020-03-29; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import uzlib
>>> print(uzlib.decompress(b'\x1f\x8b\x08\x00\xda\xe8\x81\x5e\x00\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: -3
>>> 
What am I missing?

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: decompressing a gzip file using uzlib

Post by Christian Walther » Mon Mar 30, 2020 2:02 pm

My guess is that uzlib.decompress doesn’t expect a gzip header. Docs say uzlib.DecompIO does, so maybe try that, or parse the header yourself, or if you have control over the input use a zlib header or a raw deflate stream. See RFC 1950, 1951, 1952 for specs.

spierepf
Posts: 22
Joined: Mon Jul 08, 2019 3:22 pm

Re: decompressing a gzip file using uzlib

Post by spierepf » Mon Mar 30, 2020 2:43 pm

I tried that:

Code: Select all

$ micropython
MicroPython v1.11 on 2020-03-29; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import io
>>> import uzlib
>>> file = io.BytesIO(b'\x1f\x8b\x08\x00\xda\xe8\x81\x5e\x00\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> decomp_file = uzlib.DecompIO(file)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: compression header
>>> 
Unfortunately, I don't have much control over the input. My options are .tar.gz or .zip.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: decompressing a gzip file using uzlib

Post by Christian Walther » Mon Mar 30, 2020 4:30 pm

A wbits argument of 24..31 is required to indicate a gzip header:

Code: Select all

MicroPython v1.12 on 2019-12-20; TinyPICO with ESP32-PICO-D4
Type "help()" for more information.
>>> import io
>>> import uzlib
>>> file = io.BytesIO(b'\x1f\x8b\x08\x00\xda\xe8\x81\x5e\x00\x03\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> decomp_file = uzlib.DecompIO(file, 31)
>>> decomp_file.read()
b''
See also what upip does to decompress .tar.gz.

Post Reply