{"id":15905003,"url":"https://github.com/jonnor/micropython-zipfile","last_synced_at":"2025-10-06T19:03:06.374Z","repository":{"id":256052709,"uuid":"854210084","full_name":"jonnor/micropython-zipfile","owner":"jonnor","description":"zipfile module ported from CPython to MicroPython","archived":false,"fork":false,"pushed_at":"2024-11-04T20:48:15.000Z","size":584,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T15:17:51.529Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonnor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["jonnor"]}},"created_at":"2024-09-08T17:03:50.000Z","updated_at":"2025-03-03T20:45:51.000Z","dependencies_parsed_at":"2024-10-28T09:04:54.470Z","dependency_job_id":"2bc1b21a-849e-4101-8246-d0b040fd526c","html_url":"https://github.com/jonnor/micropython-zipfile","commit_stats":{"total_commits":40,"total_committers":1,"mean_commits":40.0,"dds":0.0,"last_synced_commit":"ea45be2a6752dcba8ac9cdfcdacb0c097be30d05"},"previous_names":["jonnor/micropython-zipfile"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonnor%2Fmicropython-zipfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonnor%2Fmicropython-zipfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonnor%2Fmicropython-zipfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonnor%2Fmicropython-zipfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonnor","download_url":"https://codeload.github.com/jonnor/micropython-zipfile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217155,"owners_count":21066634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-06T13:00:23.467Z","updated_at":"2025-10-06T19:03:01.333Z","avatar_url":"https://github.com/jonnor.png","language":"Python","readme":"\n[![Tests](https://github.com/jonnor/micropython-zipfile/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/jonnor/micropython-zipfile/actions/workflows/tests.yaml)\n\n# micropython-zipfile\n\nSupport for [.zip files](https://en.wikipedia.org/wiki/ZIP_(file_format)) for [MicroPython](https://micropython.org/).\nPort of CPython [zipfile](https://docs.python.org/3/library/zipfile.html).\n\nA subset of .zip was standardized in ISO/IEC 21320–1:2015 \"Document Container File: Core\".\nSuch files should work with this module.\nZIP archives are used as a basis for many common files, such as EPUB, DOCX\n\nmicropython-zipfile was initially made for [micropython-npyfile](https://github.com/jonnor/micropython-npyfile/), to support Numpy .npz files (uses a ZIP archive).\nWhich again was made for [emlearn-micropython](https://github.com/emlearn/emlearn-micropython),\na Machine Learning and Digital Signal Processing library for MicroPython.\n\n## Status\n**Minimally useful**.\n\n- Supports read/write of ZIP archives, with or without compression\n- Highly compatible with the CPython API\n- Passes over 100 tests from the CPython test suite\n- Tested on the Unix port and ESP32 port, running MicroPython 1.23\n\n## License\n\nThe majority of the code is copied from the CPython 3.12 implementation of `zipfile`.\nThat code is, to the best of our understanding, under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2.\nAll modifications/adaptations done in this project can be used under the same license.\n\n## Features\n\nThis is an adapted implementation of the original [zipfile module](https://docs.python.org/3/library/zipfile.html) in CPython.\nWith the exception of the **Limitations** documented below, it should have the same features.\n\n#### Prerequisites\n\nThis library uses the MicroPython [deflate module](https://docs.micropython.org/en/latest/library/deflate.html#deflate.DeflateIO) to handle compression and decompression.\nTherefore your MicroPython firmware build must include this module,\nwhich may require you to enable the `MICROPY_PY_DEFLATE` and `MICROPY_PY_DEFLATE_COMPRESS` features.\n\nIf you get the error `AttributeError: 'DeflateIO' object has no attribute 'write'`, you are missing `MICROPY_PY_DEFLATE_COMPRESS`.\n\n#### Limitations\n\nThese limitations could be lifted if people contribute\n\n- Only DEFLATE and STORED (uncompressed) is supported.\n- LZMA compression *not supported*\n- BZ2 compression *not supported*\n- `PathLike` objects not supported.\n- `readline()` and `readlines()` on file objects not supported\n\nThese limitations are likely to be forever.\nThis is because they are not that relevant in a microcontroller/embedded setting.\n\n- PyFile objects not supported\n- Usage as a command-line module not supported\n- *append* mode (`'a'` option to ZipFile) not supported.\n- *exclusive* mode (`'x'` option to ZipFile) not supported.\n- Only `ascii` and `utf-8` supported for metadata encoding\n\n## Installing\n\nThis package can be installed using [mip](https://docs.micropython.org/en/latest/reference/packages.html#installing-packages-with-mip).\n\nFor example:\n\n```bash\nmpremote mip install github:jonnor/micropython-zipfile\n```\n\nOr just copy the `zipfile.py` file to your MicroPython device.\n\n## Usage\n\n#### Create a ZIP archive\n\n```python\n# Import the module\nfrom zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED\n\n# Create a .zip archive\n# for compression, swap ZIP_STORED with ZIP_DEFLATED\npath = 'myarchive.zip'\nwith ZipFile(path, 'w', ZIP_STORED) as archive: \n    contents = b'Hello World Hello World Hello World\\n'\n    with archive.open('test.txt', 'w') as f:\n        f.write(contents)\n```\n\n#### List what is inside a ZIP archive\n```python\n# List the information from a .zip archive\nprint('\\nListing information')\nwith ZipFile(path, 'r') as archive: \n    for info in archive.infolist(): \n        print(info.filename)\n        print('\\tSystem:\\t\\t' + str(info.create_system) + '(0 = Windows, 3 = Unix)') \n        print('\\tZIP version:\\t' + str(info.create_version)) \n        print('\\tCompressed:\\t' + str(info.compress_size) + ' bytes') \n        print('\\tUncompressed:\\t' + str(info.file_size) + ' bytes') \n```\n\n#### Read data from a file inside ZIP archive\n\n```python\n# Read a file from inside .zip archive\nprint('\\nReading file')\nwith ZipFile(path) as myzip:\n    with myzip.open('test.txt') as myfile:\n        print(myfile.read())\n```\n\n#### More\n\nAlso see [examples](./examples).\nAnd consult tutorials and the API reference for the CPython zipfile module.\n\n## TODO \nContributions welcomed!\n\n## Other implementations\n\n- [maruno/mpy-blox zipfile.py](https://github.com/maruno/mpy-blox/blob/master/mpy_blox/zipfile.py). Minimal implementation of `zipfile.ZipFile` which supports only ureading. The function `read()` supports uncompressed or deflate compressed data.\nNot clear if ZIP64 is supported. Not separately installable.\n\n## Developing\n\n#### Running tests on host\n\nInstall the Unix/Windows port of MicroPython.\n\nInstall modules used by the tests\n\n```\nmicropython -m mip install contextlib unittest os-path stat shutil\n```\n\nRun the basic test suite:\n\n```\nMICROPYPATH=.:$HOME/.micropython/lib micropython tests/test_zipfile.py\n```\n\nRun the full test suite with MicroPython:\n\n```\nMICROPYPATH=.:$HOME/.micropython/lib micropython -X heapsize=10M test_zipfile/test_core.py\n```\n\nThe tests can also be ran under CPython\n```\nPYTHONPATH=./ python test_zipfile/test_core.py  -v\nMICROPYPATH=.:$HOME/.micropython/lib micropython tests/test_zipfile.py\n```\n\n#### Running tests on device\n\nConnect a MicroPython device via USB.\n\nCopy over the data\n```\nmpremote cp zipfile.py :\nmpremote run tests/test_zipfile.py\n```\n\n","funding_links":["https://github.com/sponsors/jonnor"],"categories":["Libraries","Recently Updated"],"sub_categories":["Communications","[Oct 12, 2024](/content/2024/10/12/README.md)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonnor%2Fmicropython-zipfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonnor%2Fmicropython-zipfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonnor%2Fmicropython-zipfile/lists"}