tar = tarfile.TarFile('test.tar', 'w')
string = StringIO.StringIO()
string.write('hello')
string.seek(0)
info = tarfile.TarInfo(name='dir')
info.type = tarfile.DIRTYPE
info.mode = 493
info.mtime = time.time()
tar.addfile(tarinfo=info)
info = tarfile.TarInfo(name='dir/foo')
info.size = len(string.buf)
info.mtime = time.time()
tar.addfile(tarinfo=info, fileobj=string)
tar.close()
How can I programmatically create a tar archive of nested directories and files solely from Python strings and without temporary files?