problems with tarfile.open and tar.bz2

J

justin.vanwinkle

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
 
R

Richard Lewis

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
 
J

justin.vanwinkle

r is supposed to autodetect the archive type.

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

=?iso-8859-1?q?Lars_Gust=E4bel?=

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()
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top