12.4. zipfile — Work with ZIP archives¶
New in version 1.6.
Source code: Lib/zipfile.py
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.
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. filename may be a file or file-like object too.Changed in version 2.7: Support for file and file-like objects.
-
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
zlibmodule. 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).Changed in version 2.6: 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 be
ZIP_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.7.1: If the file is created with mode
'a'or'w'and thenclosedwithout adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.ZipFile is also a context manager and therefore supports the
withstatement. In the example, myzip is closed after thewithstatement’s suite is finished—even if an exception occurs:with ZipFile('spam.zip', 'w') as myzip: myzip.write('eggs.txt')
New in version 2.7: Added the ability to use
ZipFileas a context manager.
-
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.
