Skip to content

Instantly share code, notes, and snippets.

@xaf
Created February 11, 2017 18:19
Show Gist options
  • Select an option

  • Save xaf/16f7a718e4d1b0f09bd56f63b8a3323f to your computer and use it in GitHub Desktop.

Select an option

Save xaf/16f7a718e4d1b0f09bd56f63b8a3323f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import StringIO
import tarfile
tar = tarfile.open("test.tgz","w:gz")
string = StringIO.StringIO()
string.write("hello")
string.seek(0)
info = tarfile.TarInfo(name='foo')
info.size=len(string.buf)
tar.addfile(tarinfo=info, fileobj=string)
info = tarfile.TarInfo(name='bar')
info.type = tarfile.DIRTYPE
tar.addfile(tarinfo=info)
string2 = StringIO.StringIO()
string2.write("hello2")
string2.seek(0)
info = tarfile.TarInfo(name='bar/baz')
info.size=len(string2.buf)
tar.addfile(tarinfo=info, fileobj=string2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment