tars and zips

Robert Amesz rcameszREMOVETHIS at dds.removethistoo.nl
Fri Apr 13 11:31:24 EDT 2001


Bob Purvy wrote:

> [Zip]
>
> Anyway, for zip:  I've gotten as far as importing zipfile, creating
> the ZipFile object, and listing its contents.  But do I need to
> write some Python code to iterate through the files, doing read for
> each one and then writing it?  I haven't found any single 'inflate'
> method anywhere, nor the equivalent of 'tar xf'.

Use the read(<name>) method on the ZipFile-object, where <name> is the 
full name of any of the files in the archive. The one flaw is that 
read() stores all of that decompressed file into memory, and returns it 
as a string, so for very large files that may be a nuisance.

Please keep in mind that what seems to be a path specification in 
<name> actually is just part of the name: a name like 
'this/is/my/file.txt' is a good name for an archive member, but may not 
be a good filename if the subdirectories don't exist already.

For compressing files, use write(filename[, arcname[, compress_type]]). 
You'd expect to see write() working the exact same way as read(), only 
the other way around, but it doesn't: it appends a file to the archive, 
not a string. Pretty convenient, if somewhat inconsistent. Although 
<arcname> defaults to <filename> it is best to clean it up a bit so 
that it doesn't start with a '/' or something like 'C:/', and you may 
want to remove (part of) the common prefix when appending a large 
number of files: if all of the files start with, for instance, 
/usr/robert/projects/python/zip/ just keeping the prefix zip/ will 
usually be sufficient.


Also, unzipping a file sometimes (it's rare, but it happens) fails 
unexpectedly, where other archivers (like WinZip and PowerZip) don't 
complain. (The error seems to come from the zlib-library, not the Zip-
module.) However, this has never happened with files I have created 
myself, either using the zip-module, or some other archiver, so I'm at 
a loss why that is. It could be that there are some minor 
incompatibilities between the deflate-methods in zlib and some 
archivers. It's a minor issue, though: finding a truly corrupted 
zipfile, where all archivers will report CRC errors, happens at least 
as often.

Robert Amesz



More information about the Python-list mailing list