Create TarFile using string buffers

Lars Gustäbel lars at gustaebel.de
Mon Mar 19 15:42:47 EDT 2007


On Mon, Mar 19, 2007 at 12:06:39PM -0700, aurora00 at gmail.com wrote:
> I have a program that generates a number of files that will be
> packaged into a tarball. Can I stream the content into TarFile without
> first writing them out to the file system? All add(), addfile() and
> gettarinfo() seems to assume there is a file in the disk. But for me I
> seems inefficient to write all the content to the disk and then have
> it read back by the TarFile module.

addfile()'s fileobj argument can be anything that has a read()
method. The amount of bytes to read is taken from the
tarinfo.size attribute. You could let your program write its
data to a StringIO object and pass that object to addfile().

http://docs.python.org/lib/tarfile-objects.html

-- 
Lars Gustäbel
lars at gustaebel.de

Linux is like a wigwam - no Gates, no Windows, Apache inside.



More information about the Python-list mailing list