Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: create a compressed tar file for streamed writing

I need to produce the tar.gzipped text file. Is there a way to create a file for constant writing (to be able to do something like compressedFile.write("some text")), or do I need to create a raw text file first, and compress it aftewards?

This will be quite unfortunate, as the file should be really long and well compressable.

like image 786
Morse Avatar asked Apr 15 '11 17:04

Morse


1 Answers

Here's an example of how to write a compressed tarfile from a Python script:

import StringIO
import tarfile

tar = tarfile.open('example.tar.gz', 'w:gz')

# create a file record
data = StringIO.StringIO('this is some text')
info = tar.tarinfo()
info.name = 'foo.txt'
info.uname = 'pat'
info.gname = 'users'
info.size = data.len

# add the file to the tar and close it
tar.addfile(info, data)
tar.close()

Result:

% tar tvf example.tar.gz
-rw-r--r--  0 pat    users       17 Dec 31  1969 foo.txt
like image 164
samplebias Avatar answered Oct 10 '22 00:10

samplebias