# 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)
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.