Common interface for archive manipulation
Project description
arlib: Common interface for archive manipulation
Table of Contents
Rationale
Sometimes, we want 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 solutions of above problem 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
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
Built Distribution
Hashes for arlib-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddf6d7e08ec5f5119e7ed437a886258ce7209b04b3f7476857d8e5e8dd6c780c |
|
MD5 | 5836270d8a032d01539ec8344c51659d |
|
BLAKE2b-256 | dba153226d8d40fd96eeaebd4c179d51818b3c3ba92c484b95023021eb18d351 |