Skip to main content

Common interface for archive manipulation

Project description

arlib: Common interface for archive manipulation

Build Status Documentation Status Coverage Status

Table of Contents

Rationale

Sometimes, we need to deal with different archive files. There are several packages/modules for archive file manipulation, e.g., zipfile for "*.zip" files, tarfile for "*.tar.gz" or "*.tar.bz2" files, etc. If we want to support different archive type in our project, probably we need to do the following:

if zipfile.is_zipfile(file):
    ar = zipfile.ZipFile(file)
    f = ar.open('member-name')
    # some processing
elif zipfile.is_tarfile(file):
    ar = tarfile.open(file)
    f = ar.extractfile('member-name')
    # some processing
else:
    # other stuffs

The problems of the above approach are:

  • We need repeat the above code everywhere we want to support different archive types.
  • Different archive manipulation modules (e.g. zipfile and tarfile) may have different API convention.

arlib is designed to solve the above problems. It abstracts the logic of archive manipulations and provides a single high level interface for users.

Installation

Install from PyPI

pip install arlib

Install from Anaconda

conda install -c liyugong arlib

Simple example

The simplest way to use arlib is through the arlib.Archive class.

Open archive

By construct an arlib.Archive object, we can open an archive file for read or write.

The constructor of arlib.Archive also works as a factory which automatically create engine with type determined by the file properties and the mode argument. Therefore,

ar = arlib.Archive('abc.tar.gz', 'r')

will create an object of type arlib.TarArchive, and

ar = arlib.Archive('abc.zip', 'r')

will create an object of type arlib.ZipArchive if the corresponding files exist and are real tar or zip files.

List member names

The property member_names will return a list of the names of members contained in the archive, e.g.,

print(ar.member_names)

Open a member

Use the method open_member to open a member in the archive as a file object

with ar.open_member('a.txt', 'r') as f:
    # do sth by using f as an opened file object

License

The arlib package is released under the MIT License

Documentation

https://arlib.readthedocs.io/en/latest/

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

arlib-0.0.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

arlib-0.0.2-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file arlib-0.0.2.tar.gz.

File metadata

  • Download URL: arlib-0.0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for arlib-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b056b6af71bdf64654932661f861b2a24a9af3ea20dc8fb89b33cfde53f03ae7
MD5 21339d2339a40caa8adb561d45695647
BLAKE2b-256 178f53bad3c8ebc264aa71d71451e3c7ef6c4f80fc7358ae597fbdbe87fb428b

See more details on using hashes here.

File details

Details for the file arlib-0.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: arlib-0.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for arlib-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 13ff610488c3930eb39488ecca1afbd8b0d1045bb8acbc3a4309b273eef6a97c
MD5 5ea1448fe16eb3c58061056131858c60
BLAKE2b-256 a2e29c9445017ebf541a8403a7f9aa06c848986373ea169549ee29cf0911ffdf

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