Skip to main content

A Pathlib.PurePosixPath-like object for accessing the contents of ZIP archives

Project description

zippathlib - Provides a pathlib.Path subclass 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-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.

Features

  • ZipPath class provides a pathlib-like interface to 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.
  • 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].

List the root directory of a ZIP archive

$ python -m zippathlib files/frmeshfull154.zip
Directory: files\frmeshfull154.zip::
Contents:
  [D] frmeshfull154

List the files in a directory

$ python -m zippathlib files/frmeshfull154.zip frmeshfull154
Directory: files/frmeshfull154.zip::frmeshfull154
Contents:
  [F] gen00.dat
  [F] gen01.dat
  [F] gen02.dat
  [F] gen03.dat
  [F] gen04.dat

List the first few lines of a file

$ python -m zippathlib files/frmeshfull154.zip frmeshfull154/gen02.dat
File: files\frmeshfull154.zip::frmeshfull154/gen02.dat
Content:
[
[
Q([(-1618,12647),(-629,8485),(3130,8887),(3048,11037)],[11606295,14889819,13131311,9905975]),
Q(...

Extract a file from a ZIP archive to the local filesystem

# if outputdir is omitted, file is extracted to the current directory
$ python -m zippathlib files/frmeshfull154.zip frmeshfull154/gen02.dat --extract --outputdir /tmp

Extract a file from a ZIP archive to stdout

# if outputdir is "-", file is extracted and printed to stdout
$ python -m zippathlib files/frmeshfull154.zip frmeshfull154/gen02.dat --extract --outputdir -

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)

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.

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.1.0.tar.gz (13.2 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.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for zippathlib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23509f07d47a293d7c8be9c2c422129b6380e2a5472931f6c467a62fd65366b9
MD5 899f4bceca2e48032da266f1389590cb
BLAKE2b-256 2689ce025c7d8185087e9f5e72ced69acf193105f46076ff06c2954dddb16e5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zippathlib-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2fb18b3ffcae0f81ae48878736dd4f068ec0ee9238f7b7061de28067481286be
MD5 2a8f3e9f6eac3f76ba0154108eed47a1
BLAKE2b-256 8fbe5785e66544cd5c87125b592051c9f696d9802f66116d06185c7821a7ff4d

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