Zip File Extractor

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
SureshVakati
Posts: 42
Joined: Fri Feb 24, 2017 3:52 pm

Zip File Extractor

Post by SureshVakati » Tue Feb 28, 2017 3:42 pm

Hello,
Are there any libraries available to extract '.ZIP' or '.RAR' files in SD card?


User avatar
deshipu
Posts: 1385
Joined: Thu May 28, 2015 5:54 pm

Re: Zip File Extractor

Post by deshipu » Tue Feb 28, 2017 6:07 pm

That's a "no", but there is one for TAR files: https://github.com/micropython/micropyt ... r/utarfile

User avatar
SureshVakati
Posts: 42
Joined: Fri Feb 24, 2017 3:52 pm

Re: Zip File Extractor

Post by SureshVakati » Tue Feb 28, 2017 6:16 pm

Thank you!

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Zip File Extractor

Post by dhylands » Tue Feb 28, 2017 9:21 pm

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.

User avatar
SureshVakati
Posts: 42
Joined: Fri Feb 24, 2017 3:52 pm

Re: Zip File Extractor

Post by SureshVakati » Tue Feb 28, 2017 9:47 pm

Hello Dave,
Are you talking about uzlib module or gzip module? Can I pass a file name as argument to uzlib.decompress(data)?

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Zip File Extractor

Post by dhylands » Tue Feb 28, 2017 10:55 pm

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.

samfallday
Posts: 2
Joined: Thu May 14, 2020 11:22 pm

Re: Zip File Extractor

Post by samfallday » Fri Jan 22, 2021 6:14 pm

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?

User avatar
dhylands
Posts: 3592
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Zip File Extractor

Post by dhylands » Fri Jan 22, 2021 6:59 pm

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.

samfallday
Posts: 2
Joined: Thu May 14, 2020 11:22 pm

Re: Zip File Extractor

Post by samfallday » Fri Jan 29, 2021 8:20 pm

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


Post Reply