zipfile | Python Batteries

Did you know that many modules in the Python standard library have __main__ sections? They’re included mainly as examples for using the standard library itself, but there’s some useful functionality in there.

You can run these examples on the command-line with python -m $module.

Today, the zipfile module:

Usage:
    zipfile.py -l zipfile.zip        # Show listing of a zipfile
    zipfile.py -t zipfile.zip        # Test if a zipfile is valid
    zipfile.py -e zipfile.zip target # Extract zipfile into target dir
    zipfile.py -c zipfile.zip src ... # Create zipfile from sources

Using nothing but the standard library, you can list, test, extract, and create “standard” zipfiles, just like this:

numfar:~ janderson$ python -m zipfile -l ZZT.ZIP
File Name                                             Modified             Size
TOWN.HI                                        1990-01-24 17:10:16         1590
TOWN.ZZT                                       1991-11-02 18:50:24        73959
CAVES.ZZT                                      1991-11-02 18:56:04        80376
DUNGEONS.ZZT                                   1991-11-02 18:56:58        71378
CITY.ZZT                                       1991-11-02 18:58:24        63943
DEMO.ZZT                                       1990-01-26 01:35:58        26465
TOUR.ZZT                                       1991-11-02 18:48:52        33941
ZZT.DAT                                        1992-01-10 16:59:24        35823
ZZT.CFG                                        1992-01-10 15:29:30           18
HELPME.EXE                                     1994-01-30 20:21:42         6288
ORDER.DOC                                      1997-09-26 15:56:28         5174
HELPME.DOC                                     1994-02-18 10:46:16         7884
PRINTME.BAT                                    1991-10-19 21:59:28          274
ZZT.EXE                                        1992-05-21 08:38:24        47764
LICENSE.DOC                                    1997-10-09 16:14:50        26112
license.txt                                    1997-10-09 16:10:30        14428
ORDER_UK.DOC                                   1997-09-26 16:43:46         4967