Alex Rivera | Logout

python write string directly to tarfile

Asked 2009-04-11T21:41:08.607
45

Is there a way to write a string directly to a tarfile? From http://docs.python.org/library/tarfile.html it looks like only files already written to the file system can be added.

Edit
Report

1 Answer

3

In my case I wanted to read from an existing tar file, append some data to the contents, and write it to a new file. Something like:

for ti in tar_in:
    buf_in = tar.extractfile(ti)
    buf_out = io.BytesIO()
    size = buf_out.write(buf_in.read())
    size += buf_out.write(other data)
    buf_out.seek(0)
    ti.size = size
    tar_out.addfile(ti, fileobj=buf_out)

Extra code is needed for handling directories and links.

answered 2013-08-03T10:21:11.760

Your Answer