Zipping and Unzipping files

Peter Hansen peter at engcorp.com
Fri Nov 21 16:10:25 EST 2003


Doug Tolton wrote:
> 
> Is there a simple way to zip and unzip files?
> 
> I'm looking for something along the lines of:
> 
> zfile = zipfile(r'c:\somefile.zip')
> zfile.extract(r'c:\somefiles')
> 
> I've looked at the documentation for zlib and zipfile, and they seem
> pretty comprehensive, but also extremely low level.  If needed, I can
> probably make a workable component from them, but I was wondering if
> there is one already written that I'm just missing.

Probably not, since it's likely to require slight differences in 
each application.  Try this (untested):

import os, zipfile
def extract(self, todir=''):
    for name in self.namelist():
        f = open(os.path.join(todir, name), 'wb')
        f.write(self.read(name))
        f.close()

zipfile.ZipFile.extract = extract

Now you should be able to execute the two example lines you showed above...

-Peter




More information about the Python-list mailing list