{"id":18585422,"url":"https://github.com/drivendataorg/repro-zipfile","last_synced_at":"2025-04-10T13:30:59.750Z","repository":{"id":187781947,"uuid":"674831131","full_name":"drivendataorg/repro-zipfile","owner":"drivendataorg","description":"A tiny, zero-dependency replacement for Python's zipfile.ZipFile for creating reproducible/deterministic ZIP archives.","archived":false,"fork":false,"pushed_at":"2025-03-20T03:19:30.000Z","size":50,"stargazers_count":14,"open_issues_count":3,"forks_count":5,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-20T03:38:39.625Z","etag":null,"topics":["deterministic-zip","python","zip","zip-archive","zipfile"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drivendataorg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","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}},"created_at":"2023-08-04T22:46:02.000Z","updated_at":"2025-03-20T02:57:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"c92a3e5b-217f-4f60-8a49-23d578ecbd2b","html_url":"https://github.com/drivendataorg/repro-zipfile","commit_stats":null,"previous_names":["drivendataorg/repro-zipfile"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Frepro-zipfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Frepro-zipfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Frepro-zipfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drivendataorg%2Frepro-zipfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drivendataorg","download_url":"https://codeload.github.com/drivendataorg/repro-zipfile/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248225707,"owners_count":21068078,"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":["deterministic-zip","python","zip","zip-archive","zipfile"],"created_at":"2024-11-07T00:33:57.866Z","updated_at":"2025-04-10T13:30:57.716Z","avatar_url":"https://github.com/drivendataorg.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# repro-zipfile\n\n[![PyPI](https://img.shields.io/pypi/v/repro-zipfile.svg)](https://pypi.org/project/repro-zipfile/)\n[![Conda Version](https://img.shields.io/conda/vn/conda-forge/repro-zipfile.svg)](https://anaconda.org/conda-forge/repro-zipfile)\n[![conda-forge feedstock](https://img.shields.io/badge/conda--forge-feedstock-yellowgreen)](https://github.com/conda-forge/repro-zipfile-feedstock)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/repro-zipfile)](https://pypi.org/project/repro-zipfile/)\n[![tests](https://github.com/drivendataorg/repro-zipfile/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/drivendataorg/repro-zipfile/actions/workflows/tests.yml?query=branch%3Amain)\n[![codecov](https://codecov.io/gh/drivendataorg/repro-zipfile/branch/main/graph/badge.svg)](https://codecov.io/gh/drivendataorg/repro-zipfile)\n\n**A tiny, zero-dependency replacement for Python's `zipfile.ZipFile` library for creating reproducible/deterministic ZIP archives.**\n\n\"Reproducible\" or \"deterministic\" in this context means that the binary content of the ZIP archive is identical if you add files with identical binary content in the same order. It means you can reliably check equality of the contents of two ZIP archives by simply comparing checksums of the archive using a hash function like MD5 or SHA-256.\n\nThis Python package provides a `ReproducibleZipFile` class that works exactly like [`zipfile.ZipFile`](https://docs.python.org/3/library/zipfile.html#zipfile-objects) from the Python standard library, except that certain file metadata are set to fixed values. See [\"How does repro-zipfile work?\"](#how-does-repro-zipfile-work) below for details.\n\nYou can also optionally install a command-line program, **rpzip**. See [\"rpzip command line program\"](#rpzip-command-line-program) below for more information.\n\n_Looking instead to create reproducible/deterministic tar archives? Check out our sister package, [repro-tarfile](https://github.com/drivendataorg/repro-tarfile)!_\n\n## Installation\n\nrepro-zipfile is available from PyPI. To install, run:\n\n```bash\npip install repro-zipfile\n```\n\nIt is also available from conda-forge. To install, run:\n\n```bash\nconda install repro-zipfile -c conda-forge\n```\n\n## Usage\n\nSimply import `ReproducibleZipFile` and use it in the same way you would use [`zipfile.ZipFile`](https://docs.python.org/3/library/zipfile.html#zipfile-objects) from the Python standard library.\n\n```python\nfrom repro_zipfile import ReproducibleZipFile\n\nwith ReproducibleZipFile(\"archive.zip\", \"w\") as zp:\n    # Use write to add a file to the archive\n    zp.write(\"examples/data.txt\", arcname=\"data.txt\")\n    # Or writestr to write data to the archive\n    zp.writestr(\"lore.txt\", data=\"goodbye\")\n```\n\nNote that files must be written to the archive in the same order to reproduce an identical archive. Be aware that functions that like `os.listdir`, `os.glob`, `Path.iterdir`, and `Path.glob` return files in a nondeterministic order—you should call `sorted` on their returned values first.\n\nSee [`examples/usage.py`](./examples/usage.py) for an example script that you can run, and [`examples/demo_vs_zipfile.py`](./examples/demo_vs_zipfile.py) for a demonstration in contrast with the standard library's zipfile module.\n\nFor more advanced usage, such as customizing the fixed metadata values, see the subsections under [\"How does repro-zipfile work?\"](#how-does-repro-zipfile-work).\n\n## rpzip command-line program\n\n[![PyPI](https://img.shields.io/pypi/v/rpzip.svg)](https://pypi.org/project/rpzip/)\n\nYou can optionally install a lightweight command-line program, **rpzip**. This includes an additional dependency on the [typer](https://typer.tiangolo.com/) CLI framework. You can install it either directly or using the `cli` extra with repro-zipfile. We recommend you use [pipx](https://github.com/pypa/pipx) for installing Python CLIs into isolated virtual environments. You can also install it with regular pip, too.\n\n```bash\npipx install rpzip\n# or\npipx install repro-zipfile[cli]\n```\n\nrpzip is designed to a partial drop-in replacement ubiquitous [zip](https://linux.die.net/man/1/zip) program. Use `rpzip --help` to see the documentation. Here are some usage examples:\n\n```bash\n# Archive a single file\nrpzip archive.zip examples/data.txt\n# Archive multiple files\nrpzip archive.zip examples/data.txt README.md\n# Archive multiple files with a shell glob\nrpzip archive.zip examples/*.py\n# Archive a directory recursively\nrpzip -r archive.zip examples\n```\n\nIn addition to the fixed file metadata done by repro-zipfile, rpzip will also always sort all paths being written.\n\n## How does repro-zipfile work?\n\nZIP archives are not normally reproducible even when containing files with identical content because of file metadata. In particular, the usual culprits are:\n\n1. Last-modified timestamps\n2. File-system permissions (mode)\n\n`repro_zipfile.ReproducibleZipFile` is a subclass of `zipfile.ZipFile` that overrides the `write`, `writestr`, and `mkdir` methods with versions that set the above metadata to fixed values. Note that repro-zipfile does not modify the original files—only the metadata written to the archive.\n\nYou can effectively reproduce what `ReproducibleZipFile` does with something like this:\n\n```python\nfrom zipfile import ZipFile\n\nwith ZipFile(\"archive.zip\", \"w\") as zp:\n    # Use write to add a file to the archive\n    zp.write(\"examples/data.txt\", arcname=\"data.txt\")\n    zinfo = zp.getinfo(\"data.txt\")\n    zinfo.date_time = (1980, 1, 1, 0, 0, 0)\n    zinfo.external_attr = 0o644 \u003c\u003c 16\n    # Or writestr to write data to the archive\n    zp.writestr(\"lore.txt\", data=\"goodbye\")\n    zinfo = zp.getinfo(\"lore.txt\")\n    zinfo.date_time = (1980, 1, 1, 0, 0, 0)\n    zinfo.external_attr = 0o644 \u003c\u003c 16\n```\n\nIt's not hard to do, but we believe `ReproducibleZipFile` is sufficiently more convenient to justify a small package!\n\nSee the next two sections for more details about the replacement metadata values and how to customize them.\n\n### Last-modified timestamps\n\nZIP archives store the last-modified timestamps of files and directories. `ReproducibleZipFile` will set this to a fixed value. By default, the fixed value is 1980-01-01 00:00 UTC, which is the earliest timestamp that is supported by the ZIP format specifications.\n\nYou can customize this value with the `SOURCE_DATE_EPOCH` environment variable. If set, it will be used as the fixed value instead. This should be an integer corresponding to the [Unix epoch time](https://en.wikipedia.org/wiki/Unix_time) of the timestamp you want to set, e.g., `1704067230` for 2024-01-01 00:00:00 UTC. `SOURCE_DATE_EPOCH` is a [standard](https://reproducible-builds.org/docs/source-date-epoch/) created by the [Reproducible Builds project](https://reproducible-builds.org/) for software distributions.\n\n### File-system permissions\n\nZIP archives store the file-system permissions of files and directories. The default permissions set for new files or directories often can be different across different systems or users without any intentional choices being made. (These default permissions are controlled by something called [`umask`](https://en.wikipedia.org/wiki/Umask).) `ReproducibleZipFile` will set these to fixed values. By default, the fixed values are `0o644` (`rw-r--r--`) for files and `0o755` (`rwxr-xr-x`) for directories, which matches the common default `umask` of `0o022` for root users on Unix systems. (The [`0o` prefix](https://docs.python.org/3/reference/lexical_analysis.html#integers) is how you can write an octal—i.e., base 8—integer literal in Python.)\n\nYou can customize these values using the environment variables `REPRO_ZIPFILE_FILE_MODE` and `REPRO_ZIPFILE_DIR_MODE`. They should be in three-digit octal [Unix numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation), e.g., `644` for `rw-r--r--`.\n\n## Why care about reproducible ZIP archives?\n\nZIP archives are often useful when dealing with a set of multiple files, especially if the files are large and can be compressed. Creating reproducible ZIP archives is often useful for:\n\n- **Building a software package.** This is a development best practice to make it easier to verify distributed software packages. See the [Reproducible Builds project](https://reproducible-builds.org/) for more explanation.\n- **Working with data.** Verify that your data pipeline produced the same outputs, and avoid further reprocessing of identical data.\n- **Packaging machine learning model artifacts.** Manage model artifact packages more effectively by knowing when they contain identical models.\n\n## Related Tools and Alternatives\n\n- https://diffoscope.org/\n    - Can do a rich comparison of archive files and show what specifically differs\n- https://github.com/timo-reymann/deterministic-zip\n    - Command-line program written in Go that matches zip's interface but strips nondeterministic metadata when adding files\n- https://github.com/bboe/deterministic_zip\n    - Command-line program written in Python that creates deterministic ZIP archives\n- https://salsa.debian.org/reproducible-builds/strip-nondeterminism\n    - Perl library for removing nondeterministic metadata from file archives\n- https://github.com/Code0x58/python-stripzip\n    - Python command-line program that removes file metadata from existing ZIP archives\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrivendataorg%2Frepro-zipfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrivendataorg%2Frepro-zipfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrivendataorg%2Frepro-zipfile/lists"}