Source

sphinx / Doc-3k / library / zipfile.rst

Full commit

:mod:`zipfile` --- Work with ZIP archives

The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.

This module does not currently handle ZIP files which have appended comments, or multi-disk ZIP files. It can handle ZIP files that use the ZIP64 extensions (that is ZIP files that are more than 4 GByte in size). It supports decryption of encrypted files in ZIP archives, but it cannot currently create an encrypted file.

The available attributes of this module are:

The class for reading and writing ZIP files. See section :ref:`zipfile-objects` for constructor details.

Class for creating ZIP archives containing Python libraries.

Class used to represent information about a member of an archive. Instances of this class are returned by the :meth:`getinfo` and :meth:`infolist` methods of :class:`ZipFile` objects. Most users of the :mod:`zipfile` module will not need to create these, but only use those created by this module. filename should be the full name of the archive member, and date_time should be a tuple containing six fields which describe the time of the last modification to the file; the fields are described in section :ref:`zipinfo-objects`.

ZipFile Objects

Open a ZIP file, where file can be either a path to a file (a string) or a file-like object. The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, or 'a' to append to an existing file. If mode is 'a' and file refers to an existing ZIP file, then additional files are added to it. If file does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file, such as :file:`python.exe`. Using

cat myzip.zip >> python.exe

also works, and at least :program:`WinZip` can read such files. If mode is a and the file does not exist at all, it is created. compression is the ZIP compression method to use when writing the archive, and should be :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` is specified but the :mod:`zlib` module is not available, :exc:`RuntimeError` is also raised. The default is :const:`ZIP_STORED`. If allowZip64 is True zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If it is false (the default) :mod:`zipfile` will raise an exception when the ZIP file would require ZIP64 extensions. ZIP64 extensions are disabled by default because the default :program:`zip` and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support these extensions.

The following data attribute is also available:

PyZipFile Objects

The :class:`PyZipFile` constructor takes the same parameters as the :class:`ZipFile` constructor. Instances have one method in addition to those of :class:`ZipFile` objects.

ZipInfo Objects

Instances of the :class:`ZipInfo` class are returned by the :meth:`getinfo` and :meth:`infolist` methods of :class:`ZipFile` objects. Each object stores information about a single member of the ZIP archive.

Instances have the following attributes: