Skip to main content

A `pathlib.Path`-like class for accessing the contents of ZIP archives

Project description

zippathlib - Provides a pathlib.Path-like class for accessing files in ZIP archives

zippathlib is a Python library that provides a standalone ZipPath class for working with files inside ZIP archives using a familiar pathlib.Path-like interface. This allows you to navigate and access files within a ZIP archive without first extracting them. From your Python code, you can access the contents using the familar pathlib.Path API, instead of the standard library's zipfile module, and perform operations like reading, writing, checking existence of files and directories, etc.

zippathlib is also accessible from the command line, allowing you to list contents of ZIP archives, extract individual files or directories, and view the contents of the ZIP archive as a tree.

Features

  • ZipPath class provides a pathlib-like API to access ZIP archive files and directories.
  • Supports path composition using the '/' operator.
  • Supports basic read/write operations on files within a ZIP archive, including opening files for reading, writing data, etc.
  • Allows navigating the directory structure within a ZIP archive using familiar pathlib methods like iterdir(), joinpath(), etc. riterdir() provides a recursive listing of the directories and files in the archive.
  • Command-line interface for browsing a ZIP archive's contents, or for extracting files to the local filesystem.

Usage - Command line

The zippathlib module can be run from the command line with zippathlib [options] ZIP_FILE [PATH].

$ zippathlib -h
usage: zippathlib [-h] [--tree] [--extract [OUTPUTDIR]] [--limit LIMIT]
                  zip_file [path_within_zip]

positional arguments:
  zip_file              Zip file to explore
  path_within_zip       Path within the zip file (optional)

options:
  -h, --help            show this help message and exit
  --tree                list all files in a tree-like format
  --extract, -x [OUTPUTDIR]
                        extract files from zip file to a directory or '-' for
                        stdout, default is '.'
  --limit LIMIT         guard value against malicious ZIP files that uncompress
                        to excessive sizes; specify as an integer or float value
                        optionally followed by a multiplier suffix K,M,G,T,P,E,
                        or Z; default is 2.00G

List the root directory of a ZIP archive

$ zippathlib .\dist\zippathlib-0.1.0-py3-none-any.whl
Directory: dist\zippathlib-0.1.0-py3-none-any.whl::
Contents:
  [D] zippathlib
  [D] zippathlib-0.1.0.dist-info

List the files in a directory

$ zippathlib .\dist\zippathlib-0.1.0-py3-none-any.whl zippathlib-0.1.0.dist-info
Directory: dist\zippathlib-0.1.0-py3-none-any.whl::zippathlib-0.1.0.dist-info
Contents:
  [D] licenses
  [F] METADATA
  [F] WHEEL
  [F] entry_points.txt
  [F] top_level.txt
  [F] RECORD

List the first few lines of a file

$ zippathlib .\dist\zippathlib-0.1.0-py3-none-any.whl zippathlib-0.1.0.dist-info/licenses/LICENSE
File: dist\zippathlib-0.1.0-py3-none-any.whl::zippathlib-0.1.0.dist-info/licenses/LICENSE
Content:
MIT License

Copyright (c) 2025 Paul McGuire

Permission is hereby granted, free of charge, to a...

Extract a file from a ZIP archive to the local filesystem

# if outputdir is omitted, file is extracted to the current directory
$ zippathlib .\dist\zippathlib-0.1.0-py3-none-any.whl zippathlib-0.1.0.dist-info/licenses/LICENSE --extract /tmp

Extract a file from a ZIP archive to stdout

# if outputdir is "-", file is extracted and printed to stdout
$ zippathlib .\dist\zippathlib-0.1.0-py3-none-any.whl zippathlib-0.1.0.dist-info/licenses/LICENSE --extract -
MIT License

Copyright (c) 2025 Paul McGuire

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
...

View the contents of a ZIP archive (or a subdirectory within the ZIP archive) as a tree

$ python -m zippathlib ./dist/zippathlib-0.1.0-py3-none-any.whl --tree

├── zippathlib-0.1.0.dist-info
│   ├── RECORD
│   ├── top_level.txt
│   ├── entry_points.txt
│   ├── WHEEL
│   ├── METADATA
│   └── licenses
│       └── LICENSE
└── zippathlib
    ├── zip_pathlib.py
    ├── __main__.py
    └── __init__.py 

(Thanks to Will McGugan's rich library for making the tree output so easy.)

Usage - zippathlib.ZipPath API

Here are some examples on how to use ZipPath in your Python code:

Open a file from a ZIP archive for reading

from zippathlib import ZipPath

zip_path = ZipPath('archive.zip', 'path/to/file.txt')
with zip_path.open() as f:
    content = f.read()

Store a file into a ZIP archive, and use the '/' operator to navigate or create directories

from zippathlib import ZipPath

zp = ZipPath('archive.zip')
content_file = zp / 'some/directory' / 'file.txt'
content_file.write_text("This is too easy!")

List all files in a directory within the ZIP

from zippathlib import ZipPath

zip_path = ZipPath('archive.zip')
for file in (zip_path / 'some' / 'directory').iterdir():
    print(file)

Recursively list all directories and files in the ZIP

from zippathlib import ZipPath

zip_path = ZipPath('archive.zip')

# list all contents, in depth first search
for file in zip_path.riterdir():
    print(file)

Check if a file exists in the ZIP

from zippathlib import ZipPath

if ZipPath('archive.zip', 'path/to/file.txt').exists():
    print("File exists")

Installation

You can install ZipPath using pip:

pip install zippathlib

Testing

Tests are located in the tests directory and can be run using pytest:

pytest tests

Contributing

Contributions are welcome. Please open an issue or submit a pull request on GitHub.

If you have ideas or suggestions, feel free to drop a note in the GitHub repo Discussions.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zippathlib-0.3.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zippathlib-0.3.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file zippathlib-0.3.0.tar.gz.

File metadata

  • Download URL: zippathlib-0.3.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0rc2

File hashes

Hashes for zippathlib-0.3.0.tar.gz
Algorithm Hash digest
SHA256 21aada42c0594ac4cc3270093e704e4ceeaf497b7d1b9872bc3245e8222e5e24
MD5 f53b76b1b5074ffab88449d6480e170e
BLAKE2b-256 06e8305ae5ef2a26771d54b1e622f41ad00f85b43373a77c85ace91ab107ff7c

See more details on using hashes here.

File details

Details for the file zippathlib-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: zippathlib-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0rc2

File hashes

Hashes for zippathlib-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91f562019c7a642c97eef36e39a55d154160b07d538a77ec2ea68d384c2f0cf4
MD5 13b8618afa3049aa2d280267bf702d90
BLAKE2b-256 c94e4f89be0e60015b2fb43a2502844286e5d41b35ff10690689bdb657d84e6f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page