Is there a possibility to create a file directly in a tar archive?
Context: I have a method which creates content of some kind as String. I want to save this content as a file in the tar archive. Do I have to create a tmpfile or is there a possibility to create a file directly in the tar archive.
def save_files_to_tar(tarname):
archive = tarfile.open(tarname, mode='w')
for _ in range(some_number):
content = get_content()
# HERE add content to tar
I think you should use StringIO, to create a file like in-memory object, and use tarInfo to describe a fake file, like so :
import StringIO
import tarfile
archive = tarfile.open(tarname, mode='w')
for _ in range(some_number):
content = get_content()
s = StringIO.StringIO()
s.write(content)
s.seek(0)
tarinfo = tarfile.TarInfo(name="my filename")
tarinfo.size = len(s.buf)
archive.addfile(tarinfo=tarinfo, fileobj=s)
archive.close()
Hope this helps.
A solution that is Python 2 & 3 compatible using context managers:
from __future__ import unicode_literals
import tarfile
import time
from contextlib import closing
from io import BytesIO
message = 'Lorem ipsum dolor sit amet.'
filename = 'test.txt'
with tarfile.open('test.tar', 'w') as tf:
with closing(BytesIO(message.encode())) as fobj:
tarinfo = tarfile.TarInfo(filename)
tarinfo.size = len(fobj.getvalue())
tarinfo.mtime = time.time()
tf.addfile(tarinfo, fileobj=fobj)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With