471,337 Members | 1,019 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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.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 '05 #1
3 2636

On 26 Aug 2005 09:05:29 -0700, ju**************@gmail.com 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
Aug 26 '05 #2
r is supposed to autodetect the archive type.

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

Aug 26 '05 #3
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
la**@gustaebel.de

Aug 28 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hans-Joachim Widmaier | last post: by
8 posts views Thread by Jay Donnell | last post: by
8 posts views Thread by Dennis Hotson | last post: by
1 post views Thread by Beowulf | last post: by
1 post views Thread by Matthew Thorley | last post: by
3 posts views Thread by Anurag | last post: by
6 posts views Thread by Terry Carroll | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.