problems with tarfile.open and tar.bz2

Lars Gustäbel lars at gustaebel.de
Sun Aug 28 03:22:42 EDT 2005


On Fri, 26 Aug 2005 09:05:29 -0700, justin.vanwinkle wrote:

> Hello everyone,
> 
> I need some tar functionality for my program.  Currently the following
> code properly displays tar archives, and tar.gz archives.  However, it
> chokes on tar.bz2 archives with the following error:
> 
>   File "mail_filter.py", line 262, in extract_archive
>     tar_archive = tarfile.open('', 'r', fileobj)
>   File "/usr/lib/python2.3/tarfile.py", line 900, in open
>     return func(name, "r", fileobj)
>   File "/usr/lib/python2.3/tarfile.py", line 980, in bz2open
>     raise ValueError, "no support for external file objects"
> ValueError: no support for external file objects

The problem here is that the bz2.BZ2File class that tarfile.py uses to
access tar.bz2 files has no support for an external file-object argument.
In contrast to gzip.GzipFile, it works solely on real files.
This limitation is somewhat annoying, but so far no one took the trouble
changing the bz2 module.

If your program does not rely on random access to the tar.bz2 file, you
can still use the "r|bz2" stream mode:

tar_archive = tarfile.open(mode="r|bz2", fileobj=StringIO.StringIO(attach))
for tarinfo in tar_archive:
    print tarinfo.name
    print tar_archive.extractfile(tarinfo).read()
tar_archive.close()

-- 
Lars Gustäbel
lars at gustaebel.de




More information about the Python-list mailing list