Trouble unzipping a file on a Raspberry Pi Pico using uzlib

Hi,

I was wondering if anyone might be able to shed some light on an issue I am having. I am trying to expand a zip file on a raspberry pi pico with micropython and not having much success. I am using the uzlib module, which after some googling seems to be the way to go. However, the code is failing with a -3 error which I believe is something to do with not being able to understand the format of the zip file.

Traceback (most recent call last):
  File "<stdin>", line 20, in <module>
  File "<stdin>", line 10, in unzip
ValueError: -3

The test zip I am using is very simple and was created from the command line using:

touch text.txt
zip test.zip text.txt

It also doesn’t work with the third party zip I also ultimately want to use.

The code to unzip the file is as follows:

import uzlib
import os

def unzip(zip_file_path, extract_path):

  with open(zip_file_path, "rb") as zip_file:
    zip_data = zip_file.read()

    # Decompress the zip data.
    try:
        files = uzlib.decompress(zip_data)
    except:
        print("Decompress failed!")

    # Extract the files to the specified extract path.
    try:
        for filename, data in files.items():
          with open(os.path.join(extract_path, filename), "wb") as output_file:
            output_file.write(data)
    except:
        print("Write failed!")

zip_file_path = "test.zip"
extract_path = "/tmp"

unzip(zip_file_path, extract_path)

Any ideas or better ways to unzip a file would be gratefully received.

I should probably also say that the files are all in the root dir of the pico.

Hi Andrew,

Looks like zip has a few options available for compression, a good first step might be to manually switch to those and see if the Pico likes any of them:


(Snip taken from a site mirroring the manpage)

Maybe having a full size Pi Zero in the mix talking over serial to a Pico? IME big OS-y stuff like networking and file systems are best handled by microprocessors, but I’m not saying you need to abandon your plan!

Could you share a little more about your project, and what the motivation is? That’ll help us suggest alternative plans of attack.

Thanks for your reply James.

I’ve tried different zip configs with no effect. I’m a bit stumped with this one as all info I have suggests it should work.

With regards to my project, I am wanting to download the latest release of my Pico app via the GitHub api onto the pico directly over wifi, then unzip it and restart the Pico to use the new version. Everything is working except the unzip bit.

A ZIP file is an archive format. They may often use zlib for compression, but they contain more than that. In particular, there is a header for the archive, as well a header for each file that is inside, that specifies where the (compressed) file data is - along with metadata such as file creation time, etc. There are also checksums to detect any corruption. So to read or write .zip files one needs an implementation that understands this structure.
There is such a module for MicroPython now: GitHub - jonnor/micropython-zipfile: zipfile module ported from CPython to MicroPython
I ported it from CPython on the desktop, so it should work with most files - as long as they use zlib “deflate” compression (or no compression - “stored”).

3 Likes

Hi @Jon281344, Welcome to the Forums!!!

Thanks for sharing this resource, I hope it helps some users out with their issues.

1 Like

Thank you, Aaron :slight_smile: Hopefully it will help someone!