tar = tarfile.open('example.tar.gz', 'w:gz')
data = StringIO.StringIO('this is some text')
info = tar.tarinfo()
info.name = 'foo.txt'
info.uname = 'pat'
info.gname = 'users'
info.size = data.len
tar.addfile(info, data)
tar.close()
Python: create a compressed tar file for streamed writing