Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add file to tar archive without saving it first

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
like image 227
PKuhn Avatar asked Jun 01 '16 11:06

PKuhn


2 Answers

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.

like image 63
Ronan Avatar answered Sep 18 '22 16:09

Ronan


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)
like image 39
Cas Avatar answered Sep 19 '22 16:09

Cas