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 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 currently cannot create an encrypted file. Decryption is extremely slow as it is implemented in native Python rather than C.
For other archive formats, see the bz2, gzip, and
tarfile modules.
The module defines the following items:
-
exception
zipfile.BadZipfile¶ The error raised for bad ZIP files (old name:
zipfile.error).
-
exception
zipfile.LargeZipFile¶ The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled.
-
class
zipfile.ZipFile¶ The class for reading and writing ZIP files. See section ZipFile Objects for constructor details.
-
class
zipfile.PyZipFile¶ Class for creating ZIP archives containing Python libraries.
-
class
zipfile.ZipInfo([filename[, date_time]])¶ Class used to represent information about a member of an archive. Instances of this class are returned by the
getinfo()andinfolist()methods ofZipFileobjects. Most users of thezipfilemodule 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 ZipInfo Objects.
-
zipfile.is_zipfile(filename)¶ Returns
Trueif filename is a valid ZIP file based on its magic number, otherwise returnsFalse.
-
zipfile.ZIP_STORED¶ The numeric constant for an uncompressed archive member.
-
zipfile.ZIP_DEFLATED¶ The numeric constant for the usual ZIP compression method. This requires the zlib module. No other compression methods are currently supported.
See also
- PKZIP Application Note
- Documentation on the ZIP file format by Phil Katz, the creator of the format and algorithms used.
- Info-ZIP Home Page
- Information about the Info-ZIP project’s ZIP archive programs and development libraries.
12.4.1. ZipFile Objects¶
-
class
zipfile.ZipFile(file[, mode[, compression[, allowZip64]]]) 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 aspython.exe. Usingcat myzip.zip >> python.exe
also works, and at least WinZip can read such files. If mode is
aand the file does not exist at all, it is created. compression is the ZIP compression method to use when writing the archive, and should beZIP_STOREDorZIP_DEFLATED; unrecognized values will causeRuntimeErrorto be raised. IfZIP_DEFLATEDis specified but thezlibmodule is not available,RuntimeErroris also raised. The default isZIP_STORED. If allowZip64 isTruezipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If it is false (the default)zipfilewill raise an exception when the ZIP file would require ZIP64 extensions. ZIP64 extensions are disabled by default because the default zip and unzip commands on Unix (the InfoZIP utilities) don’t support these extensions.Changed in version 2.6:
Changed in version 2.6: If the file does not exist, it is created if the mode is ‘a’.
-
ZipFile.close()¶ Close the archive file. You must call
close()before exiting your program or essential records will not be written.
-
ZipFile.getinfo(name)¶ Return a
ZipInfoobject with information about the archive member name. Callinggetinfo()for a name not currently contained in the archive will raise aKeyError.
-
ZipFile.infolist()¶ Return a list containing a
ZipInfoobject for each member of the archive. The objects are in the same order as their entries in the actual ZIP file on disk if an existing archive was opened.
-
ZipFile.namelist()¶ Return a list of archive members by name.
-
ZipFile.open(name[, mode[, pwd]])¶ Extract a member from the archive as a file-like object (ZipExtFile). name is the name of the file in the archive, or a
ZipInfoobject. The mode parameter, if included, must be one of the following:'r'(the default),'U', or'rU'. Choosing'U'or'rU'will enable universal newline support in the read-only object. pwd is the password used for encrypted files. Callingopen()on a closed ZipFile will raise aRuntimeError.Note
The file-like object is read-only and provides the following methods:
read(),readline(),readlines(),__iter__(),next().Note
If the ZipFile was created by passing in a file-like object as the first argument to the constructor, then the object returned by
open()shares the ZipFile’s file pointer. Under these circumstances, the object returned byopen()should not be used after any additional operations are performed on the ZipFile object. If the ZipFile was created by passing in a string (the filename) as the first argument to the constructor, thenopen()will create a new file object that will be held by the ZipExtFile, allowing it to operate independently of the ZipFile.Note
The
open(),read()andextract()methods can take a filename or aZipInfoobject. You will appreciate this when trying to read a ZIP file that contains members with duplicate names.New in version 2.6:
New in version 2.6.