problems with tarfile.open and tar.bz2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • justin.vanwinkle@gmail.com

    #1

    problems with tarfile.open and tar.bz2

    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.get names()
    for archive_file in tar_archive.get members():
    print archive_file
    print tar_archive.ext ractfile(archiv e_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

  • Richard Lewis

    #2
    Re: problems with tarfile.open and tar.bz2


    On 26 Aug 2005 09:05:29 -0700, justin.vanwinkl e@gmail.com said:[color=blue]
    > 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.get names()
    > for archive_file in tar_archive.get members():
    > print archive_file
    > print tar_archive.ext ractfile(archiv e_file).read()
    > #except:
    > # print 'Error while extracting %s.' % (attach_type)
    > # return ''
    >[/color]
    Try opening with the 'r:bz2' mode instead of just 'r'.

    Cheers,
    Richard

    Comment

    • justin.vanwinkle@gmail.com

      #3
      Re: problems with tarfile.open and tar.bz2

      r is supposed to autodetect the archive type.

      However, even changing it to 'r:bz2' produces an identical error.

      Comment

      • Lars Gustäbel

        #4
        Re: problems with tarfile.open and tar.bz2

        On Fri, 26 Aug 2005 09:05:29 -0700, justin.vanwinkl e wrote:
        [color=blue]
        > 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[/color]

        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(mo de="r|bz2", fileobj=StringI O.StringIO(atta ch))
        for tarinfo in tar_archive:
        print tarinfo.name
        print tar_archive.ext ractfile(tarinf o).read()
        tar_archive.clo se()

        --
        Lars Gustäbel
        lars@gustaebel. de

        Comment

        Working...