Skip to main content

A package to provide pathlib like access to zip & tar archives.

Project description

archive-path

Build Status codecov.io PyPI version Conda Version

A package to provide pathlib like access to zip & tar archives.

Installation

$ pip install archive-path

Usage

For reading zip (ZipPath) or tar (TarPath) files:

from archive_path import TarPath, ZipPath

path = TarPath("path/to/file.tar.gz", mode="r:gz")

sub_path = path / "folder" / "file.txt"
assert sub_path.filepath == "path/to/file.tar.gz"
assert sub_path.at == "folder/file.txt"
assert sub_path.exists() and sub_path.is_file()
assert sub_path.parent.is_dir()
content = sub_path.read_text()

for sub_path in path.iterdir():
    print(sub_path)

For writing files, you should use within a context manager, or directly call the close method:

with TarPath("path/to/file.tar.gz", mode="w:gz") as path:

    (path / "new_file.txt").write_text("hallo world")
    # there are also some features equivalent to shutil
    (path / "other_file.txt").putfile("path/to/external_file.txt")
    (path / "other_folder").puttree("path/to/external_folder", pattern="**/*")

Note that archive formats do not allow to overwrite existing files (they will raise a FileExistsError).

For performant access to single files:

from archive_path import read_file_in_tar, read_file_in_zip

content = read_file_in_tar("path/to/file.tar.gz", "file.txt", encoding="utf8")

These methods allow for faster access to files (using less RAM) in archives containing 1000's of files. This is because, the archive's file index is only read until the path is found (discarding non-matches), rather than the standard tarfile/zipfile approach that is to read the entire index into memory first.

Windows compatibility

Paths within the archives are always read and written as being / delimited. This means that the package works on Windows, but will not be compatible with archives written outside this package with \\ path delimiters.

Development

See CONTRIBUTING.md for details on how to contribute to this package.

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

archive-path-0.3.2.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

archive_path-0.3.2-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file archive-path-0.3.2.tar.gz.

File metadata

  • Download URL: archive-path-0.3.2.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for archive-path-0.3.2.tar.gz
Algorithm Hash digest
SHA256 6ca00a877589e8fa78db9ee87fa60260cab1a10172a4e6c5be43727513a72659
MD5 411d6f60ca6ad86363d065dc007a8b56
BLAKE2b-256 66079af88275bfc47d941b5b2e1e6774a447bbdb93febc1a49ab091995d24daf

See more details on using hashes here.

Provenance

File details

Details for the file archive_path-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for archive_path-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 96eda5eb8a1332f80b5cad9a306be0fe005a45174d0df0601b91d9b8c800de49
MD5 1e29f01eb26eb85e741056dbf0d8e107
BLAKE2b-256 844d1504b37fb078a1c01b226512c808a2dd3a8e92dce83b650726af8018f83c

See more details on using hashes here.

Provenance

Supported by

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