# Trouble unzipping a file on a Raspberry Pi Pico using uzlib

**URL:** <https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869>\
**Category:** Micropython\
**Created:** [24 August 2023 01:13 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869 "2023-08-24T01:13:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew138558](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/andrew138558/32/6856_2.png) [@Andrew138558](https://forum.core-electronics.com.au/u/Andrew138558)\
**Post date:** [24 August 2023 01:13 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869/1 "2023-08-24T01:13:23Z")

</div>

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.

```auto
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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![James](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/james/32/4400_2.png) [@James](https://forum.core-electronics.com.au/u/James)\
**Post date:** [24 August 2023 01:55 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869/3 "2023-08-24T01:55:05Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/a/aa9ec0a63b45df2e606446ad2e62738a0ab1ed59.png)  
(Snip taken from a [site mirroring the manpage](https://linux.die.net/man/1/zip))

> [@Andrew138558](#):
>
> Any ideas or better ways to unzip a file would be gratefully received.

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.

---

<div class="post-metadata">

**Author:** ![Andrew138558](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/andrew138558/32/6856_2.png) [@Andrew138558](https://forum.core-electronics.com.au/u/Andrew138558)\
**Post date:** [24 August 2023 06:28 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869/4 "2023-08-24T06:28:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jon281344](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jon281344/32/11050_2.png) [@Jon281344](https://forum.core-electronics.com.au/u/Jon281344)\
**Post date:** [11 October 2024 18:16 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869/5 "2024-10-11T18:16:17Z")

</div>

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](https://github.com/jonnor/micropython-zipfile)  
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”).

---

<div class="post-metadata">

**Author:** ![Aaron](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/aaron/32/9861_2.png) [@Aaron](https://forum.core-electronics.com.au/u/Aaron)\
**Post date:** [14 October 2024 00:54 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869/6 "2024-10-14T00:54:25Z")

</div>

Hi @Jon281344, Welcome to the Forums!!!

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

---

<div class="post-metadata">

**Author:** ![Jon281344](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jon281344/32/11050_2.png) [@Jon281344](https://forum.core-electronics.com.au/u/Jon281344)\
**Post date:** [14 October 2024 07:36 UTC](https://forum.core-electronics.com.au/t/trouble-unzipping-a-file-on-a-raspberry-pi-pico-using-uzlib/17869/7 "2024-10-14T07:36:20Z")

</div>

Thank you, Aaron 🙂 Hopefully it will help someone!
