arlib: Common interface for archive manipulation (Tar, Zip, etc)
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 abstract class arlib.Archive defines the common interface to handle different archive types, such as tar file, zip file or an directory. Three concrete classes arlib.TarArchive, arlib.ZipArchive and arlib.DirArchive implement the interface correspondingly.
Open archive
The simplest way to open an archive is using arlib.open function
ar = arlib.open('abc.tar.gz', 'r')
This will determine the type of the archive automatically and return a corresponding object of one of the three engine classes. If we don't want the automatic engine determination mechanism, we can also specify the class via argument engine, e.g.
ar = arlib.open('abc.tar.gz', 'r', engine=ZipArchive)
or we can simply construct an object of the engine class
ar = arlib.ZipArchive('abc.tar.gz', 'r')
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)
Check member
Use the method member_is_dir and member_is_file to check whether a member is a directory or a regular file
ar = arlib.open('abc.tar', 'r')
ar.member_is_dir('member_name')
ar.member_is_file('member_name')
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
Extract members to a location
Use the method extract to extract members to a specified location
ar = arlib.open('abc.tar', 'r')
ar.extract() # extract all the members to the current working directory
ar.extract(path='d:/hello', members=['abc.txt', 'dir1/'])
License
The arlib package is released under the MIT License
Documentation
Metadata
Release files for arlib 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| arlib-0.0.4.tar.gz | 8.2 kB | Without provenance | Details for arlib-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| arlib-0.0.4-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for arlib-0.0.4-py2.py3-none-any.whl |
Total release size: 16.0 kB
Release files / arlib-0.0.4.tar.gz
| Download URL | arlib-0.0.4.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1ebd9f77da1c6323f0340c5444643b753c2f0dda9c396afedb49d2538180cb9c
|
|
BLAKE2b-256 checksum How to use checksums |
7869541eb2f17dc1992bb391095a50775b7d2b4c9510905f6f9d2c0dd4996699
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8
|
Release files / arlib-0.0.4-py2.py3-none-any.whl
| Download URL | arlib-0.0.4-py2.py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
7e537bee5a157f5a1f1b06ec10d0ff662ca40ddf673362ce2ef352b20dbd4fad
|
|
BLAKE2b-256 checksum How to use checksums |
62fed500a4b80583f87ce742defb2bea161a7485642da52331eb0c7d4e7c1b58
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8
|
Security for arlib 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.