0

Is there a way to tarred a folder and get a tarred stream instead of a tarred file? I have tried to use tar module but it directly return the tarred file.

with tarfile.open("zipped.tar",'w|') as tar:
    for base_root, subFolders, files in os.walk('test'):
            for j in files:
                filepath = os.path.join(base_root,j)
                if os.path.isfile(filepath):
                    with open(filepath, 'rb') as file:
                        size = os.stat(filepath).st_size
                        info = tarfile.TarInfo()
                        info.size = size
                        info.name = filepath
                        if(size <= chunck_size):
                            data = file.read(info.size)
                            fobj = StringIO.StringIO(data)
                            tar.addfile(info, fobj)
                        else:
                            data = ""
                            while True:
                                temp_data = file.read(chunck_size)
                                if temp_data == '':
                                    break
                                data = data + temp_data
                            fobj = StringIO.StringIO(data)
                            tar.addfile(info, fobj) 
Dan D.
  • 73,243
  • 15
  • 104
  • 123
Robin W.
  • 361
  • 1
  • 3
  • 14

1 Answers1

1

According to the documentation, open can take a fileobj argument :

If fileobj is specified, it is used as an alternative to a file object opened in binary mode for name. It is supposed to be at position 0.

So you can write this, then use the buffer object.

import io
buffer = io.BytesIO()
with tarfile.open("zipped.tar",'w|', fileobj=buffer) as tar:
madjar
  • 12,691
  • 2
  • 44
  • 52