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)
About the Author