simple python script to zip files

John Machin sjmachin at lexicon.net
Sat Feb 16 15:53:15 EST 2008


On Feb 17, 6:52 am, Tim Chase <python.l... at tim.thechases.com> wrote:
> > Thanks! Works indeed. Strange thing is though, the files created are the
> > exact size as the original file. So it seems like it is zipping without
> > compression.
>
> The instantiation of the ZipFile object can take an optional
> parameter to control the compression.  The zipfile module only
> supports storing (the default as you discovered) and "deflation":
>
>   f = zipfile.ZipFile(zipfilename, 'w',
>         compression=zipfile.ZIP_DEFLATED)
>
> You can read more at
>
> http://docs.python.org/lib/zipfile-objects.html

Confession: An infrequent user of the zipfile module, I've been caught
twice by this strange choice of default argument.

I wonder how many people actually need/want the stored (uncompressed )
format. The select few must surely document that their choice is
deliberate:

f = zipfile.ZipFile(
    zipfilename,
    mode='w',
    compression=zipfile.ZIP_STORED, # no compression
    # Yes the above is deliberate.
    # See section n.nn of project specs.
    )

Suggestion: for the benefit of folk who (unlike the OP and I) do read
the manual, it might help if the text had one paragraph per arg, and
included a warning:
"""
... also works, and at least WinZip can read such files.

/compression/ is the ZIP compression method to use when writing the
archive, and should be ZIP_STORED or ZIP_DEFLATED. *WARNING:* the
default is ZIP_STORED (no compression). Unrecognized values will cause
RuntimeError to be raised. If ZIP_DEFLATED is specified but the zlib
module is not available, RuntimeError is also raised.

If /allowZip64/ is True ...
"""

Cheers,
John



More information about the Python-list mailing list