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]] 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 '.'

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.2.0.tar.gz (14.6 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.2.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for zippathlib-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1e285d2d21c69e884a48e12af5fc2e59cd504e57fb01023f365ca2ed53b4ad21
MD5 c6b8181a941987e2d143bdd6af594aa0
BLAKE2b-256 255e7dcdf6eb980b08e1b00509c599801b9d2e9fdeb535d8cfa03ead1325825e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for zippathlib-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c8a046ad4b4b74376bbb890c59ddde34fed3aa95235dd74d76784f3980bd5ee
MD5 619207ef47f1848a41378ca8e23002de
BLAKE2b-256 baccd69e13fecc3559931234dd792ce41bb8ce06378bcf9ffe88c8ba0f3e8c6e

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