Python includes a zipfile module, which uses zlib to give you read and
write access to zipfiles.  Info-ZIP unzip, the standard unzip for
Unix, is in Debian non-free for some reason, probably because its
license doesn't meet the Debian Free Software Guidelines; this is a
free-software alternative.

Of course, that's not why I wrote it; I wrote it to get familiar with
the zipfile module, because I was going to use it in some proprietary
software I'm writing for a client.

#!/usr/local/bin/python
# learn how to use zipfile module

import sys, zipfile, os, os.path

def unzip_file_into_dir(file, dir):
    os.mkdir(dir, 0777)
    zfobj = zipfile.ZipFile(file)
    for name in zfobj.namelist():
        if name.endswith('/'):
            os.mkdir(os.path.join(dir, name))
        else:
            outfile = open(os.path.join(dir, name), 'wb')
            outfile.write(zfobj.read(name))
            outfile.close()

def main():
    unzip_file_into_dir(open(sys.argv[1]), sys.argv[2])

if __name__ == '__main__': main()

Reply via email to