Create gziped archives in memory. UserPageVisits:2710 active 80 80 DaniWeb 561 60 2010-01-22T23:56:22+00:00

Create gziped archives in memory.


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.

About the Author

Mind explorer.

code snippet
# tested with python 2.6 and 3.1
# See also the standard modules tarfile and gzip

import tarfile
    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 = = "w:gz", fileobj = file_out)
    for p in path:
    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 = "r:gz", fileobj = file_in)