Skip to main content

Common interface for archive manipulation

Project description

arlib: Common interface for archive manipulation (Tar, Zip, etc)

Build Status Documentation Status Coverage Status

Table of Contents


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 ='member-name')
    # some processing
elif zipfile.is_tarfile(file):
    ar =
    f = ar.extractfile('member-name')
    # some processing
    # 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.


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 function

ar ='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 ='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.,


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 ='abc.tar', 'r')

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 ='abc.tar', 'r')
ar.extract() # extract all the members to the current working directory
ar.extract(path='d:/hello', members=['abc.txt', 'dir1/'])


The arlib package is released under the MIT License


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for arlib, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size arlib-0.0.4-py2.py3-none-any.whl (7.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size arlib-0.0.4.tar.gz (8.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page