stringio+tarfile

superpollo user at example.net
Fri Jul 3 13:42:39 EDT 2009


Peter Otten wrote:

> gettarinfo() expects a real file, not a file-like object.
> You have to create your TarInfo manually.

ok... which attributes are mandatory, and which optional?


> I recommend that you have a look into the tarfile module's source code.
>

i will try... but:


$ cat /usr/lib/python2.3/tarfile.py | wc -l
1938

wow! it'll take some time ;-)


> The following seems to work:
> 
> import sys
> import time
> import tarfile
> import StringIO
> 
> sf1 = "first.txt", StringIO.StringIO("one one\n")
> sf2 = "second.txt", StringIO.StringIO("two\n")
> tf = StringIO.StringIO()
> 
> tar = tarfile.open(fileobj=tf , mode="w")
> 
> mtime = time.time()
> for name, f in [sf1 , sf2]:
>     ti = tarfile.TarInfo(name)
>     ti.size = f.len
>     ti.mtime = mtime
>     # add more attributes as needed 
>     tar.addfile(ti, f)
> 
> sys.stdout.write(tf.getvalue())
> 
> Peter
> 

much obliged mr otten

bye



More information about the Python-list mailing list