Page 1 of 2

Zip File Extractor

Posted: Tue Feb 28, 2017 3:42 pm
by SureshVakati
Hello,
Are there any libraries available to extract '.ZIP' or '.RAR' files in SD card?

Re: Zip File Extractor

Posted: Tue Feb 28, 2017 6:06 pm
by deshipu

Re: Zip File Extractor

Posted: Tue Feb 28, 2017 6:07 pm
by deshipu
That's a "no", but there is one for TAR files: https://github.com/micropython/micropyt ... r/utarfile

Re: Zip File Extractor

Posted: Tue Feb 28, 2017 6:16 pm
by SureshVakati
Thank you!

Re: Zip File Extractor

Posted: Tue Feb 28, 2017 9:21 pm
by dhylands
The decompressor for zipfile is available. So if you're up to parsing the zip file format, then you could write one in pure python.

I've got C code that was never merged that does pieces of it on MicroPython. Frozen files wound up being more useful.

Re: Zip File Extractor

Posted: Tue Feb 28, 2017 9:47 pm
by SureshVakati
Hello Dave,
Are you talking about uzlib module or gzip module? Can I pass a file name as argument to uzlib.decompress(data)?

Re: Zip File Extractor

Posted: Tue Feb 28, 2017 10:55 pm
by dhylands
uzlib is what zip uses. No it doesn't take a filename. You'd need to parse the zip file format and extract the compressed data and feed that into zlib.decompress.

Re: Zip File Extractor

Posted: Fri Jan 22, 2021 6:14 pm
by samfallday
Hello All, following up on this as I am trying to do the same (extract contents of a zip). Forgive my noobness but is this thread saying that you can somehow use uzlib to decompress a .zip file? I have a Digi Xbee 3 running micropython v.1.12. and have a .zip file with three files in it (certificates with the file extension .pem).

Went here but this is all C code?
https://github.com/pfalcon/uzlib

Tried using this in my project but it's evidently not complete/working.
https://pypi.org/project/micropython-cpython-uzlib/

Any suggestions?

Re: Zip File Extractor

Posted: Fri Jan 22, 2021 6:59 pm
by dhylands
uzlib only knows about the compression format used to compress a file inside a zip file. It doesn't know how to read a zip file though.

So you need to write some code to parse the ZIP file headers and such and get to the compressed data for a file. Once you've got that compressed data you can use uzlib to decompress that data.

Re: Zip File Extractor

Posted: Fri Jan 29, 2021 8:20 pm
by samfallday
Thanks for the reply dhylands. That's a few steps ahead of where I'm actually stuck. Just getting the uzlib module to be recognized seems to be a problem. uzlib.py from https://pypi.org/project/micropython-cpython-uzlib/ only contains the below. The other problem is that "zlib" from the import statement isn't on my micropython implementation (https://github.com/digidotcom/xbee-micropython).

Code: Select all

import micropython
from zlib import *


class DecompIO:

    def __init__(self, stream, dict_bits):
        self.stream = stream
        self.decomp = decompressobj(dict_bits)
        self.pending = b""


    def read(self, size):
        while len(self.pending) < size:
            inp = self.stream.read(256)
            if not inp:
                break
            outp = self.decomp.decompress(inp)
            self.pending += outp

        outp = self.pending[:size]
        self.pending = self.pending[size:]
        return outp