problems with tarfile.open and tar.bz2

Discussion in 'Python' started by justin.vanwinkle@gmail.com, Aug 26, 2005.

  1. Guest

    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 code snippet is:

    fileobj = StringIO(attach)

    if attach_type == 'application/x-tgz' or attach_type ==
    'application/x-tbz' or attach_type == 'application/x-tar':
    print '%s file detected, attempting to decompress' %
    (attach_type)

    tar_archive = tarfile.open('', 'r', fileobj)

    print tar_archive.getnames()
    for archive_file in tar_archive.getmembers():
    print archive_file
    print tar_archive.extractfile(archive_file).read()
    #except:
    # print 'Error while extracting %s.' % (attach_type)
    # return ''

    I'm at my wits end with this error, it doesn't make any sense that it
    would treat bzip2'd tar archives very differently than gz'd tar
    archives.

    Justin
     
    , Aug 26, 2005
    #1
    1. Advertising

  2. On 26 Aug 2005 09:05:29 -0700, said:
    > 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 code snippet is:
    >
    > fileobj = StringIO(attach)
    >
    > if attach_type == 'application/x-tgz' or attach_type ==
    > 'application/x-tbz' or attach_type == 'application/x-tar':
    > print '%s file detected, attempting to decompress' %
    > (attach_type)
    >
    > tar_archive = tarfile.open('', 'r', fileobj)
    >
    > print tar_archive.getnames()
    > for archive_file in tar_archive.getmembers():
    > print archive_file
    > print tar_archive.extractfile(archive_file).read()
    > #except:
    > # print 'Error while extracting %s.' % (attach_type)
    > # return ''
    >

    Try opening with the 'r:bz2' mode instead of just 'r'.

    Cheers,
    Richard
     
    Richard Lewis, Aug 26, 2005
    #2
    1. Advertising

  3. Guest

    r is supposed to autodetect the archive type.

    However, even changing it to 'r:bz2' produces an identical error.
     
    , Aug 26, 2005
    #3
  4. 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
     
    =?iso-8859-1?q?Lars_Gust=E4bel?=, Aug 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Johnson

    tarfile+bz2 and GnuPGInterface

    Ron Johnson, Oct 17, 2004, in forum: Python
    Replies:
    0
    Views:
    299
    Ron Johnson
    Oct 17, 2004
  2. Claudio Grondi
    Replies:
    4
    Views:
    588
    Claudio Grondi
    Aug 20, 2005
  3. Jordan

    Tarfile .bz2

    Jordan, Dec 11, 2006, in forum: Python
    Replies:
    8
    Views:
    406
    Piet van Oostrum
    Dec 13, 2006
  4. Magdoll
    Replies:
    3
    Views:
    396
    Steven D'Aprano
    Jun 5, 2010
  5. m_ahlenius
    Replies:
    9
    Views:
    567
    m_ahlenius
    Aug 21, 2010
Loading...

Share This Page