954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Create gziped archives in memory.

By Gribouillis on Jan 22nd, 2010 11:56 pm

This snippet provides 2 functions to create in memory a gziped archive of a sequence of files and directories, and to extract files from this archive. These functions are similar in effect to the linux commands tar czf and tar xzf but instead of writing an archive file on disk, a python string is used.

# tar.py
# tested with python 2.6 and 3.1
# See also the standard modules tarfile and gzip

import tarfile
try:
    from cStringIO import StringIO as BIO
except ImportError: # python 3
    from io import BytesIO as BIO

def tar_cz(*path):
    """tar_cz(*path) -> bytes
    Compress a sequence of files or directories in memory.
    The resulting string could be stored as a .tgz file."""

    file_out = BIO()
    tar = tarfile.open(mode = "w:gz", fileobj = file_out)
    for p in path:
        tar.add(p)
    tar.close()
    return file_out.getvalue()

def tar_xz(stringz, folder = "."):
    """tar_xz(stringz, folder = ".") -> None
    Uncompress a string created by tar_cz in a given directory."""

    file_in = BIO(stringz)
    tar = tarfile.open(mode= "r:gz", fileobj = file_in)
    tar.extractall(folder)

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: