diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fzipfile.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fzipfile.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fzipfile.html | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fzipfile.html b/devdocs/python~3.12/library%2Fzipfile.html new file mode 100644 index 00000000..1a36e187 --- /dev/null +++ b/devdocs/python~3.12/library%2Fzipfile.html @@ -0,0 +1,251 @@ + <span id="zipfile-work-with-zip-archives"></span><h1>zipfile — Work with ZIP archives</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/zipfile/">Lib/zipfile/</a></p> <p>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 <a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a>.</p> <p>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 GiB 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.</p> <p>The module defines the following items:</p> <dl class="py exception"> <dt class="sig sig-object py" id="zipfile.BadZipFile"> +<code>exception zipfile.BadZipFile</code> </dt> <dd> +<p>The error raised for bad ZIP files.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd> +</dl> <dl class="py exception"> <dt class="sig sig-object py" id="zipfile.BadZipfile"> +<code>exception zipfile.BadZipfile</code> </dt> <dd> +<p>Alias of <a class="reference internal" href="#zipfile.BadZipFile" title="zipfile.BadZipFile"><code>BadZipFile</code></a>, for compatibility with older Python versions.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.2.</span></p> </div> </dd> +</dl> <dl class="py exception"> <dt class="sig sig-object py" id="zipfile.LargeZipFile"> +<code>exception zipfile.LargeZipFile</code> </dt> <dd> +<p>The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py"> <em class="property">class<span class="w"> </span></em><span class="sig-prename descclassname">zipfile.</span><span class="sig-name descname">ZipFile</span> +</dt> <dd> +<p>The class for reading and writing ZIP files. See section <a class="reference internal" href="#zipfile-objects"><span class="std std-ref">ZipFile Objects</span></a> for constructor details.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py"> <em class="property">class<span class="w"> </span></em><span class="sig-prename descclassname">zipfile.</span><span class="sig-name descname">Path</span> +</dt> <dd> +<p>Class that implements a subset of the interface provided by <a class="reference internal" href="pathlib#pathlib.Path" title="pathlib.Path"><code>pathlib.Path</code></a>, including the full <a class="reference internal" href="importlib.resources.abc#importlib.resources.abc.Traversable" title="importlib.resources.abc.Traversable"><code>importlib.resources.abc.Traversable</code></a> interface.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py"> <em class="property">class<span class="w"> </span></em><span class="sig-prename descclassname">zipfile.</span><span class="sig-name descname">PyZipFile</span> +</dt> <dd> +<p>Class for creating ZIP archives containing Python libraries.</p> </dd> +</dl> <dl class="py class"> <dt class="sig sig-object py" id="zipfile.ZipInfo"> +<code>class zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))</code> </dt> <dd> +<p>Class used to represent information about a member of an archive. Instances of this class are returned by the <a class="reference internal" href="#zipfile.ZipFile.getinfo" title="zipfile.ZipFile.getinfo"><code>getinfo()</code></a> and <a class="reference internal" href="#zipfile.ZipFile.infolist" title="zipfile.ZipFile.infolist"><code>infolist()</code></a> methods of <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> objects. Most users of the <a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code>zipfile</code></a> module will not need to create these, but only use those created by this module. <em>filename</em> should be the full name of the archive member, and <em>date_time</em> should be a tuple containing six fields which describe the time of the last modification to the file; the fields are described in section <a class="reference internal" href="#zipinfo-objects"><span class="std std-ref">ZipInfo Objects</span></a>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="zipfile.is_zipfile"> +<code>zipfile.is_zipfile(filename)</code> </dt> <dd> +<p>Returns <code>True</code> if <em>filename</em> is a valid ZIP file based on its magic number, otherwise returns <code>False</code>. <em>filename</em> may be a file or file-like object too.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.1: </span>Support for file and file-like objects.</p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.ZIP_STORED"> +<code>zipfile.ZIP_STORED</code> </dt> <dd> +<p>The numeric constant for an uncompressed archive member.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.ZIP_DEFLATED"> +<code>zipfile.ZIP_DEFLATED</code> </dt> <dd> +<p>The numeric constant for the usual ZIP compression method. This requires the <a class="reference internal" href="zlib#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code>zlib</code></a> module.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.ZIP_BZIP2"> +<code>zipfile.ZIP_BZIP2</code> </dt> <dd> +<p>The numeric constant for the BZIP2 compression method. This requires the <a class="reference internal" href="bz2#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code>bz2</code></a> module.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.ZIP_LZMA"> +<code>zipfile.ZIP_LZMA</code> </dt> <dd> +<p>The numeric constant for the LZMA compression method. This requires the <a class="reference internal" href="lzma#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code>lzma</code></a> module.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The ZIP file format specification has included support for bzip2 compression since 2001, and for LZMA compression since 2006. However, some tools (including older Python releases) do not support these compression methods, and may either refuse to process the ZIP file altogether, or fail to extract individual files.</p> </div> </dd> +</dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt><a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a></dt> +<dd> +<p>Documentation on the ZIP file format by Phil Katz, the creator of the format and algorithms used.</p> </dd> <dt><a class="reference external" href="https://infozip.sourceforge.net/">Info-ZIP Home Page</a></dt> +<dd> +<p>Information about the Info-ZIP project’s ZIP archive programs and development libraries.</p> </dd> </dl> </div> <section id="zipfile-objects"> <span id="id1"></span><h2>ZipFile Objects</h2> <dl class="py class"> <dt class="sig sig-object py" id="zipfile.ZipFile"> +<code>class zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)</code> </dt> <dd> +<p>Open a ZIP file, where <em>file</em> can be a path to a file (a string), a file-like object or a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> <p>The <em>mode</em> parameter should be <code>'r'</code> to read an existing file, <code>'w'</code> to truncate and write a new file, <code>'a'</code> to append to an existing file, or <code>'x'</code> to exclusively create and write a new file. If <em>mode</em> is <code>'x'</code> and <em>file</em> refers to an existing file, a <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> will be raised. If <em>mode</em> is <code>'a'</code> and <em>file</em> refers to an existing ZIP file, then additional files are added to it. If <em>file</em> 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 <code>python.exe</code>). If <em>mode</em> is <code>'a'</code> and the file does not exist at all, it is created. If <em>mode</em> is <code>'r'</code> or <code>'a'</code>, the file should be seekable.</p> <p><em>compression</em> is the ZIP compression method to use when writing the archive, and should be <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code>ZIP_STORED</code></a>, <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code>ZIP_DEFLATED</code></a>, <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code>ZIP_BZIP2</code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code>ZIP_LZMA</code></a>; unrecognized values will cause <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> to be raised. If <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code>ZIP_DEFLATED</code></a>, <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code>ZIP_BZIP2</code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code>ZIP_LZMA</code></a> is specified but the corresponding module (<a class="reference internal" href="zlib#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code>zlib</code></a>, <a class="reference internal" href="bz2#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code>bz2</code></a> or <a class="reference internal" href="lzma#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code>lzma</code></a>) is not available, <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised. The default is <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code>ZIP_STORED</code></a>.</p> <p>If <em>allowZip64</em> is <code>True</code> (the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is <code>false</code> <a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code>zipfile</code></a> will raise an exception when the ZIP file would require ZIP64 extensions.</p> <p>The <em>compresslevel</em> parameter controls the compression level to use when writing files to the archive. When using <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code>ZIP_STORED</code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code>ZIP_LZMA</code></a> it has no effect. When using <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code>ZIP_DEFLATED</code></a> integers <code>0</code> through <code>9</code> are accepted (see <a class="reference internal" href="zlib#zlib.compressobj" title="zlib.compressobj"><code>zlib</code></a> for more information). When using <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code>ZIP_BZIP2</code></a> integers <code>1</code> through <code>9</code> are accepted (see <a class="reference internal" href="bz2#bz2.BZ2File" title="bz2.BZ2File"><code>bz2</code></a> for more information).</p> <p>The <em>strict_timestamps</em> argument, when set to <code>False</code>, allows to zip files older than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit.</p> <p>When mode is <code>'r'</code>, <em>metadata_encoding</em> may be set to the name of a codec, which will be used to decode metadata such as the names of members and ZIP comments.</p> <p>If the file is created with mode <code>'w'</code>, <code>'x'</code> or <code>'a'</code> and then <a class="reference internal" href="#zipfile.ZipFile.close" title="zipfile.ZipFile.close"><code>closed</code></a> without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.</p> <p>ZipFile is also a context manager and therefore supports the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement. In the example, <em>myzip</em> is closed after the <code>with</code> statement’s suite is finished—even if an exception occurs:</p> <pre data-language="python">with ZipFile('spam.zip', 'w') as myzip: + myzip.write('eggs.txt') +</pre> <div class="admonition note"> <p class="admonition-title">Note</p> <p><em>metadata_encoding</em> is an instance-wide setting for the ZipFile. It is not currently possible to set this on a per-member basis.</p> <p>This attribute is a workaround for legacy implementations which produce archives with names in the current locale encoding or code page (mostly on Windows). According to the .ZIP standard, the encoding of metadata may be specified to be either IBM code page (default) or UTF-8 by a flag in the archive header. That flag takes precedence over <em>metadata_encoding</em>, which is a Python-specific extension.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2: </span>Added the ability to use <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> as a context manager.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Added support for <a class="reference internal" href="bz2#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code>bzip2</code></a> and <a class="reference internal" href="lzma#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code>lzma</code></a> compression.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>ZIP64 extensions are enabled by default.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for writing to unseekable streams. Added support for the <code>'x'</code> mode.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Previously, a plain <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised for unrecognized compression values.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>file</em> parameter accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Add the <em>compresslevel</em> parameter.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>The <em>strict_timestamps</em> keyword-only argument</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Added support for specifying member name encoding for reading metadata in the zipfile’s directory and file headers.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.close"> +<code>ZipFile.close()</code> </dt> <dd> +<p>Close the archive file. You must call <a class="reference internal" href="#zipfile.ZipFile.close" title="zipfile.ZipFile.close"><code>close()</code></a> before exiting your program or essential records will not be written.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.getinfo"> +<code>ZipFile.getinfo(name)</code> </dt> <dd> +<p>Return a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object with information about the archive member <em>name</em>. Calling <a class="reference internal" href="#zipfile.ZipFile.getinfo" title="zipfile.ZipFile.getinfo"><code>getinfo()</code></a> for a name not currently contained in the archive will raise a <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.infolist"> +<code>ZipFile.infolist()</code> </dt> <dd> +<p>Return a list containing a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object 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.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.namelist"> +<code>ZipFile.namelist()</code> </dt> <dd> +<p>Return a list of archive members by name.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.open"> +<code>ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)</code> </dt> <dd> +<p>Access a member of the archive as a binary file-like object. <em>name</em> can be either the name of a file within the archive or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object. The <em>mode</em> parameter, if included, must be <code>'r'</code> (the default) or <code>'w'</code>. <em>pwd</em> is the password used to decrypt encrypted ZIP files as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object.</p> <p><a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code>open()</code></a> is also a context manager and therefore supports the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement:</p> <pre data-language="python">with ZipFile('spam.zip') as myzip: + with myzip.open('eggs.txt') as myfile: + print(myfile.read()) +</pre> <p>With <em>mode</em> <code>'r'</code> the file-like object (<code>ZipExtFile</code>) is read-only and provides the following methods: <a class="reference internal" href="io#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code>read()</code></a>, <a class="reference internal" href="io#io.IOBase.readline" title="io.IOBase.readline"><code>readline()</code></a>, <a class="reference internal" href="io#io.IOBase.readlines" title="io.IOBase.readlines"><code>readlines()</code></a>, <a class="reference internal" href="io#io.IOBase.seek" title="io.IOBase.seek"><code>seek()</code></a>, <a class="reference internal" href="io#io.IOBase.tell" title="io.IOBase.tell"><code>tell()</code></a>, <a class="reference internal" href="stdtypes#container.__iter__" title="container.__iter__"><code>__iter__()</code></a>, <a class="reference internal" href="stdtypes#iterator.__next__" title="iterator.__next__"><code>__next__()</code></a>. These objects can operate independently of the ZipFile.</p> <p>With <code>mode='w'</code>, a writable file handle is returned, which supports the <a class="reference internal" href="io#io.BufferedIOBase.write" title="io.BufferedIOBase.write"><code>write()</code></a> method. While a writable file handle is open, attempting to read or write other files in the ZIP file will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>.</p> <p>When writing a file, if the file size is not known in advance but may exceed 2 GiB, pass <code>force_zip64=True</code> to ensure that the header format is capable of supporting large files. If the file size is known in advance, construct a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object with <a class="reference internal" href="#zipfile.ZipInfo.file_size" title="zipfile.ZipInfo.file_size"><code>file_size</code></a> set, and use that as the <em>name</em> parameter.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code>open()</code></a>, <a class="reference internal" href="#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code>read()</code></a> and <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code>extract()</code></a> methods can take a filename or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object. You will appreciate this when trying to read a ZIP file that contains members with duplicate names.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Removed support of <code>mode='U'</code>. Use <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> for reading compressed text files in <a class="reference internal" href="../glossary#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> mode.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span><a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code>ZipFile.open()</code></a> can now be used to write files into the archive with the <code>mode='w'</code> option.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code>open()</code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.extract"> +<code>ZipFile.extract(member, path=None, pwd=None)</code> </dt> <dd> +<p>Extract a member from the archive to the current working directory; <em>member</em> must be its full name or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object. Its file information is extracted as accurately as possible. <em>path</em> specifies a different directory to extract to. <em>member</em> can be a filename or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object. <em>pwd</em> is the password used for encrypted files as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object.</p> <p>Returns the normalized path created (a directory or new file).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If a member filename is an absolute path, a drive/UNC sharepoint and leading (back)slashes will be stripped, e.g.: <code>///foo/bar</code> becomes <code>foo/bar</code> on Unix, and <code>C:\foo\bar</code> becomes <code>foo\bar</code> on Windows. And all <code>".."</code> components in a member filename will be removed, e.g.: <code>../../foo../../ba..r</code> becomes <code>foo../ba..r</code>. On Windows illegal characters (<code>:</code>, <code><</code>, <code>></code>, <code>|</code>, <code>"</code>, <code>?</code>, and <code>*</code>) replaced by underscore (<code>_</code>).</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code>extract()</code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>path</em> parameter accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.extractall"> +<code>ZipFile.extractall(path=None, members=None, pwd=None)</code> </dt> <dd> +<p>Extract all members from the archive to the current working directory. <em>path</em> specifies a different directory to extract to. <em>members</em> is optional and must be a subset of the list returned by <a class="reference internal" href="#zipfile.ZipFile.namelist" title="zipfile.ZipFile.namelist"><code>namelist()</code></a>. <em>pwd</em> is the password used for encrypted files as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of <em>path</em>, e.g. members that have absolute filenames starting with <code>"/"</code> or filenames with two dots <code>".."</code>. This module attempts to prevent that. See <a class="reference internal" href="#zipfile.ZipFile.extract" title="zipfile.ZipFile.extract"><code>extract()</code></a> note.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.extractall" title="zipfile.ZipFile.extractall"><code>extractall()</code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>path</em> parameter accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.printdir"> +<code>ZipFile.printdir()</code> </dt> <dd> +<p>Print a table of contents for the archive to <code>sys.stdout</code>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.setpassword"> +<code>ZipFile.setpassword(pwd)</code> </dt> <dd> +<p>Set <em>pwd</em> (a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object) as default password to extract encrypted files.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.read"> +<code>ZipFile.read(name, pwd=None)</code> </dt> <dd> +<p>Return the bytes of the file <em>name</em> in the archive. <em>name</em> is the name of the file in the archive, or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> object. The archive must be open for read or append. <em>pwd</em> is the password used for encrypted files as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object and, if specified, overrides the default password set with <a class="reference internal" href="#zipfile.ZipFile.setpassword" title="zipfile.ZipFile.setpassword"><code>setpassword()</code></a>. Calling <a class="reference internal" href="#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code>read()</code></a> on a ZipFile that uses a compression method other than <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code>ZIP_STORED</code></a>, <a class="reference internal" href="#zipfile.ZIP_DEFLATED" title="zipfile.ZIP_DEFLATED"><code>ZIP_DEFLATED</code></a>, <a class="reference internal" href="#zipfile.ZIP_BZIP2" title="zipfile.ZIP_BZIP2"><code>ZIP_BZIP2</code></a> or <a class="reference internal" href="#zipfile.ZIP_LZMA" title="zipfile.ZIP_LZMA"><code>ZIP_LZMA</code></a> will raise a <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>. An error will also be raised if the corresponding compression module is not available.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code>read()</code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.testzip"> +<code>ZipFile.testzip()</code> </dt> <dd> +<p>Read all the files in the archive and check their CRC’s and file headers. Return the name of the first bad file, or else return <code>None</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.testzip" title="zipfile.ZipFile.testzip"><code>testzip()</code></a> on a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.write"> +<code>ZipFile.write(filename, arcname=None, compress_type=None, compresslevel=None)</code> </dt> <dd> +<p>Write the file named <em>filename</em> to the archive, giving it the archive name <em>arcname</em> (by default, this will be the same as <em>filename</em>, but without a drive letter and with leading path separators removed). If given, <em>compress_type</em> overrides the value given for the <em>compression</em> parameter to the constructor for the new entry. Similarly, <em>compresslevel</em> will override the constructor if given. The archive must be open with mode <code>'w'</code>, <code>'x'</code> or <code>'a'</code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The ZIP file standard historically did not specify a metadata encoding, but strongly recommended CP437 (the original IBM PC encoding) for interoperability. Recent versions allow use of UTF-8 (only). In this module, UTF-8 will automatically be used to write the member names if they contain any non-ASCII characters. It is not possible to write member names in any encoding other than ASCII or UTF-8.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Archive names should be relative to the archive root, that is, they should not start with a path separator.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If <code>arcname</code> (or <code>filename</code>, if <code>arcname</code> is not given) contains a null byte, the name of the file in the archive will be truncated at the null byte.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>A leading slash in the filename may lead to the archive being impossible to open in some zip programs on Windows systems.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.write" title="zipfile.ZipFile.write"><code>write()</code></a> on a ZipFile created with mode <code>'r'</code> or a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.writestr"> +<code>ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)</code> </dt> <dd> +<p>Write a file into the archive. The contents is <em>data</em>, which may be either a <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> or a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> instance; if it is a <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a>, it is encoded as UTF-8 first. <em>zinfo_or_arcname</em> is either the file name it will be given in the archive, or a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance. If it’s an instance, at least the filename, date, and time must be given. If it’s a name, the date and time is set to the current date and time. The archive must be opened with mode <code>'w'</code>, <code>'x'</code> or <code>'a'</code>.</p> <p>If given, <em>compress_type</em> overrides the value given for the <em>compression</em> parameter to the constructor for the new entry, or in the <em>zinfo_or_arcname</em> (if that is a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance). Similarly, <em>compresslevel</em> will override the constructor if given.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>When passing a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance as the <em>zinfo_or_arcname</em> parameter, the compression method used will be that specified in the <em>compress_type</em> member of the given <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance. By default, the <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> constructor sets this member to <a class="reference internal" href="#zipfile.ZIP_STORED" title="zipfile.ZIP_STORED"><code>ZIP_STORED</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>compress_type</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Calling <a class="reference internal" href="#zipfile.ZipFile.writestr" title="zipfile.ZipFile.writestr"><code>writestr()</code></a> on a ZipFile created with mode <code>'r'</code> or a closed ZipFile will raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>. Previously, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> was raised.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipFile.mkdir"> +<code>ZipFile.mkdir(zinfo_or_directory, mode=511)</code> </dt> <dd> +<p>Create a directory inside the archive. If <em>zinfo_or_directory</em> is a string, a directory is created inside the archive with the mode that is specified in the <em>mode</em> argument. If, however, <em>zinfo_or_directory</em> is a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance then the <em>mode</em> argument is ignored.</p> <p>The archive must be opened with mode <code>'w'</code>, <code>'x'</code> or <code>'a'</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd> +</dl> <p>The following data attributes are also available:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipFile.filename"> +<code>ZipFile.filename</code> </dt> <dd> +<p>Name of the ZIP file.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipFile.debug"> +<code>ZipFile.debug</code> </dt> <dd> +<p>The level of debug output to use. This may be set from <code>0</code> (the default, no output) to <code>3</code> (the most output). Debugging information is written to <code>sys.stdout</code>.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipFile.comment"> +<code>ZipFile.comment</code> </dt> <dd> +<p>The comment associated with the ZIP file as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object. If assigning a comment to a <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> instance created with mode <code>'w'</code>, <code>'x'</code> or <code>'a'</code>, it should be no longer than 65535 bytes. Comments longer than this will be truncated.</p> </dd> +</dl> </section> <section id="path-objects"> <span id="id2"></span><h2>Path Objects</h2> <dl class="py class"> <dt class="sig sig-object py" id="zipfile.Path"> +<code>class zipfile.Path(root, at='')</code> </dt> <dd> +<p>Construct a Path object from a <code>root</code> zipfile (which may be a <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> instance or <code>file</code> suitable for passing to the <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> constructor).</p> <p><code>at</code> specifies the location of this Path within the zipfile, e.g. ‘dir/file.txt’, ‘dir/’, or ‘’. Defaults to the empty string, indicating the root.</p> </dd> +</dl> <p>Path objects expose the following features of <a class="reference internal" href="pathlib#pathlib.Path" title="pathlib.Path"><code>pathlib.Path</code></a> objects:</p> <p>Path objects are traversable using the <code>/</code> operator or <code>joinpath</code>.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.Path.name"> +<code>Path.name</code> </dt> <dd> +<p>The final path component.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.open"> +<code>Path.open(mode='r', *, pwd, **)</code> </dt> <dd> +<p>Invoke <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code>ZipFile.open()</code></a> on the current path. Allows opening for read or write, text or binary through supported modes: ‘r’, ‘w’, ‘rb’, ‘wb’. Positional and keyword arguments are passed through to <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> when opened as text and ignored otherwise. <code>pwd</code> is the <code>pwd</code> parameter to <a class="reference internal" href="#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code>ZipFile.open()</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for text and binary modes for open. Default mode is now text.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11.2: </span>The <code>encoding</code> parameter can be supplied as a positional argument without causing a <a class="reference internal" href="exceptions#TypeError" title="TypeError"><code>TypeError</code></a>. As it could in 3.9. Code needing to be compatible with unpatched 3.10 and 3.11 versions must pass all <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> arguments, <code>encoding</code> included, as keywords.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.iterdir"> +<code>Path.iterdir()</code> </dt> <dd> +<p>Enumerate the children of the current directory.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.is_dir"> +<code>Path.is_dir()</code> </dt> <dd> +<p>Return <code>True</code> if the current context references a directory.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.is_file"> +<code>Path.is_file()</code> </dt> <dd> +<p>Return <code>True</code> if the current context references a file.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.exists"> +<code>Path.exists()</code> </dt> <dd> +<p>Return <code>True</code> if the current context references a file or directory in the zip file.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.Path.suffix"> +<code>Path.suffix</code> </dt> <dd> +<p>The file extension of the final component.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#zipfile.Path.suffix" title="zipfile.Path.suffix"><code>Path.suffix</code></a> property.</p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.Path.stem"> +<code>Path.stem</code> </dt> <dd> +<p>The final path component, without its suffix.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#zipfile.Path.stem" title="zipfile.Path.stem"><code>Path.stem</code></a> property.</p> </div> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="zipfile.Path.suffixes"> +<code>Path.suffixes</code> </dt> <dd> +<p>A list of the path’s file extensions.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#zipfile.Path.suffixes" title="zipfile.Path.suffixes"><code>Path.suffixes</code></a> property.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.read_text"> +<code>Path.read_text(*, **)</code> </dt> <dd> +<p>Read the current file as unicode text. Positional and keyword arguments are passed through to <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> (except <code>buffer</code>, which is implied by the context).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11.2: </span>The <code>encoding</code> parameter can be supplied as a positional argument without causing a <a class="reference internal" href="exceptions#TypeError" title="TypeError"><code>TypeError</code></a>. As it could in 3.9. Code needing to be compatible with unpatched 3.10 and 3.11 versions must pass all <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> arguments, <code>encoding</code> included, as keywords.</p> </div> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.read_bytes"> +<code>Path.read_bytes()</code> </dt> <dd> +<p>Read the current file as bytes.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.Path.joinpath"> +<code>Path.joinpath(*other)</code> </dt> <dd> +<p>Return a new Path object with each of the <em>other</em> arguments joined. The following are equivalent:</p> <pre data-language="python">>>> Path(...).joinpath('child').joinpath('grandchild') +>>> Path(...).joinpath('child', 'grandchild') +>>> Path(...) / 'child' / 'grandchild' +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Prior to 3.10, <code>joinpath</code> was undocumented and accepted exactly one parameter.</p> </div> </dd> +</dl> <p>The <a class="reference external" href="https://pypi.org/project/zipp">zipp</a> project provides backports of the latest path object functionality to older Pythons. Use <code>zipp.Path</code> in place of <code>zipfile.Path</code> for early access to changes.</p> </section> <section id="pyzipfile-objects"> <span id="id3"></span><h2>PyZipFile Objects</h2> <p>The <a class="reference internal" href="#zipfile.PyZipFile" title="zipfile.PyZipFile"><code>PyZipFile</code></a> constructor takes the same parameters as the <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> constructor, and one additional parameter, <em>optimize</em>.</p> <dl class="py class"> <dt class="sig sig-object py" id="zipfile.PyZipFile"> +<code>class zipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=- 1)</code> </dt> <dd> +<div class="versionadded"> <p><span class="versionmodified added">New in version 3.2: </span>The <em>optimize</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>ZIP64 extensions are enabled by default.</p> </div> <p>Instances have one method in addition to those of <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> objects:</p> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.PyZipFile.writepy"> +<code>writepy(pathname, basename='', filterfunc=None)</code> </dt> <dd> +<p>Search for files <code>*.py</code> and add the corresponding file to the archive.</p> <p>If the <em>optimize</em> parameter to <a class="reference internal" href="#zipfile.PyZipFile" title="zipfile.PyZipFile"><code>PyZipFile</code></a> was not given or <code>-1</code>, the corresponding file is a <code>*.pyc</code> file, compiling if necessary.</p> <p>If the <em>optimize</em> parameter to <a class="reference internal" href="#zipfile.PyZipFile" title="zipfile.PyZipFile"><code>PyZipFile</code></a> was <code>0</code>, <code>1</code> or <code>2</code>, only files with that optimization level (see <a class="reference internal" href="functions#compile" title="compile"><code>compile()</code></a>) are added to the archive, compiling if necessary.</p> <p>If <em>pathname</em> is a file, the filename must end with <code>.py</code>, and just the (corresponding <code>*.pyc</code>) file is added at the top level (no path information). If <em>pathname</em> is a file that does not end with <code>.py</code>, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> will be raised. If it is a directory, and the directory is not a package directory, then all the files <code>*.pyc</code> are added at the top level. If the directory is a package directory, then all <code>*.pyc</code> are added under the package name as a file path, and if any subdirectories are package directories, all of these are added recursively in sorted order.</p> <p><em>basename</em> is intended for internal use only.</p> <p><em>filterfunc</em>, if given, must be a function taking a single string argument. It will be passed each path (including each individual full file path) before it is added to the archive. If <em>filterfunc</em> returns a false value, the path will not be added, and if it is a directory its contents will be ignored. For example, if our test files are all either in <code>test</code> directories or start with the string <code>test_</code>, we can use a <em>filterfunc</em> to exclude them:</p> <pre data-language="python">>>> zf = PyZipFile('myprog.zip') +>>> def notests(s): +... fn = os.path.basename(s) +... return (not (fn == 'test' or fn.startswith('test_'))) +... +>>> zf.writepy('myprog', filterfunc=notests) +</pre> <p>The <a class="reference internal" href="#zipfile.PyZipFile.writepy" title="zipfile.PyZipFile.writepy"><code>writepy()</code></a> method makes archives with file names like this:</p> <pre data-language="python">string.pyc # Top level name +test/__init__.pyc # Package directory +test/testall.pyc # Module test.testall +test/bogus/__init__.pyc # Subpackage directory +test/bogus/myfile.pyc # Submodule test.bogus.myfile +</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4: </span>The <em>filterfunc</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>pathname</em> parameter accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Recursion sorts directory entries.</p> </div> </dd> +</dl> </dd> +</dl> </section> <section id="zipinfo-objects"> <span id="id4"></span><h2>ZipInfo Objects</h2> <p>Instances of the <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> class are returned by the <a class="reference internal" href="#zipfile.ZipFile.getinfo" title="zipfile.ZipFile.getinfo"><code>getinfo()</code></a> and <a class="reference internal" href="#zipfile.ZipFile.infolist" title="zipfile.ZipFile.infolist"><code>infolist()</code></a> methods of <a class="reference internal" href="#zipfile.ZipFile" title="zipfile.ZipFile"><code>ZipFile</code></a> objects. Each object stores information about a single member of the ZIP archive.</p> <p>There is one classmethod to make a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance for a filesystem file:</p> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipInfo.from_file"> +<code>classmethod ZipInfo.from_file(filename, arcname=None, *, strict_timestamps=True)</code> </dt> <dd> +<p>Construct a <a class="reference internal" href="#zipfile.ZipInfo" title="zipfile.ZipInfo"><code>ZipInfo</code></a> instance for a file on the filesystem, in preparation for adding it to a zip file.</p> <p><em>filename</em> should be the path to a file or directory on the filesystem.</p> <p>If <em>arcname</em> is specified, it is used as the name within the archive. If <em>arcname</em> is not specified, the name will be the same as <em>filename</em>, but with any drive letter and leading path separators removed.</p> <p>The <em>strict_timestamps</em> argument, when set to <code>False</code>, allows to zip files older than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6.2: </span>The <em>filename</em> parameter accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>The <em>strict_timestamps</em> keyword-only argument</p> </div> </dd> +</dl> <p>Instances have the following methods and attributes:</p> <dl class="py method"> <dt class="sig sig-object py" id="zipfile.ZipInfo.is_dir"> +<code>ZipInfo.is_dir()</code> </dt> <dd> +<p>Return <code>True</code> if this archive member is a directory.</p> <p>This uses the entry’s name: directories should always end with <code>/</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.filename"> +<code>ZipInfo.filename</code> </dt> <dd> +<p>Name of the file in the archive.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.date_time"> +<code>ZipInfo.date_time</code> </dt> <dd> +<p>The time and date of the last modification to the archive member. This is a tuple of six values:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Index</p></th> <th class="head"><p>Value</p></th> </tr> </thead> <tr> +<td><p><code>0</code></p></td> <td><p>Year (>= 1980)</p></td> </tr> <tr> +<td><p><code>1</code></p></td> <td><p>Month (one-based)</p></td> </tr> <tr> +<td><p><code>2</code></p></td> <td><p>Day of month (one-based)</p></td> </tr> <tr> +<td><p><code>3</code></p></td> <td><p>Hours (zero-based)</p></td> </tr> <tr> +<td><p><code>4</code></p></td> <td><p>Minutes (zero-based)</p></td> </tr> <tr> +<td><p><code>5</code></p></td> <td><p>Seconds (zero-based)</p></td> </tr> </table> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The ZIP file format does not support timestamps before 1980.</p> </div> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.compress_type"> +<code>ZipInfo.compress_type</code> </dt> <dd> +<p>Type of compression for the archive member.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.comment"> +<code>ZipInfo.comment</code> </dt> <dd> +<p>Comment for the individual archive member as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.extra"> +<code>ZipInfo.extra</code> </dt> <dd> +<p>Expansion field data. The <a class="reference external" href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">PKZIP Application Note</a> contains some comments on the internal structure of the data contained in this <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.create_system"> +<code>ZipInfo.create_system</code> </dt> <dd> +<p>System which created ZIP archive.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.create_version"> +<code>ZipInfo.create_version</code> </dt> <dd> +<p>PKZIP version which created ZIP archive.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.extract_version"> +<code>ZipInfo.extract_version</code> </dt> <dd> +<p>PKZIP version needed to extract archive.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.reserved"> +<code>ZipInfo.reserved</code> </dt> <dd> +<p>Must be zero.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.flag_bits"> +<code>ZipInfo.flag_bits</code> </dt> <dd> +<p>ZIP flag bits.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.volume"> +<code>ZipInfo.volume</code> </dt> <dd> +<p>Volume number of file header.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.internal_attr"> +<code>ZipInfo.internal_attr</code> </dt> <dd> +<p>Internal attributes.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.external_attr"> +<code>ZipInfo.external_attr</code> </dt> <dd> +<p>External file attributes.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.header_offset"> +<code>ZipInfo.header_offset</code> </dt> <dd> +<p>Byte offset to the file header.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.CRC"> +<code>ZipInfo.CRC</code> </dt> <dd> +<p>CRC-32 of the uncompressed file.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.compress_size"> +<code>ZipInfo.compress_size</code> </dt> <dd> +<p>Size of the compressed data.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="zipfile.ZipInfo.file_size"> +<code>ZipInfo.file_size</code> </dt> <dd> +<p>Size of the uncompressed file.</p> </dd> +</dl> </section> <section id="command-line-interface"> <span id="zipfile-commandline"></span><h2>Command-Line Interface</h2> <p>The <a class="reference internal" href="#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code>zipfile</code></a> module provides a simple command-line interface to interact with ZIP archives.</p> <p>If you want to create a new ZIP archive, specify its name after the <a class="reference internal" href="#cmdoption-zipfile-c"><code>-c</code></a> option and then list the filename(s) that should be included:</p> <pre data-language="shell">$ python -m zipfile -c monty.zip spam.txt eggs.txt +</pre> <p>Passing a directory is also acceptable:</p> <pre data-language="shell">$ python -m zipfile -c monty.zip life-of-brian_1979/ +</pre> <p>If you want to extract a ZIP archive into the specified directory, use the <a class="reference internal" href="#cmdoption-zipfile-e"><code>-e</code></a> option:</p> <pre data-language="shell">$ python -m zipfile -e monty.zip target-dir/ +</pre> <p>For a list of the files in a ZIP archive, use the <a class="reference internal" href="#cmdoption-zipfile-l"><code>-l</code></a> option:</p> <pre data-language="shell">$ python -m zipfile -l monty.zip +</pre> <section id="command-line-options"> <h3>Command-line options</h3> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-zipfile-l"> +<code>-l <zipfile></code> </dt> <dt class="sig sig-object std" id="cmdoption-zipfile-list"> +<code>--list <zipfile></code> </dt> <dd> +<p>List files in a zipfile.</p> </dd> +</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-zipfile-c"> +<code>-c <zipfile> <source1> ... <sourceN></code> </dt> <dt class="sig sig-object std" id="cmdoption-zipfile-create"> +<code>--create <zipfile> <source1> ... <sourceN></code> </dt> <dd> +<p>Create zipfile from source files.</p> </dd> +</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-zipfile-e"> +<code>-e <zipfile> <output_dir></code> </dt> <dt class="sig sig-object std" id="cmdoption-zipfile-extract"> +<code>--extract <zipfile> <output_dir></code> </dt> <dd> +<p>Extract zipfile into target directory.</p> </dd> +</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-zipfile-t"> +<code>-t <zipfile></code> </dt> <dt class="sig sig-object std" id="cmdoption-zipfile-test"> +<code>--test <zipfile></code> </dt> <dd> +<p>Test whether the zipfile is valid or not.</p> </dd> +</dl> <dl class="std option"> <dt class="sig sig-object std" id="cmdoption-zipfile-metadata-encoding"> +<code>--metadata-encoding <encoding></code> </dt> <dd> +<p>Specify encoding of member names for <a class="reference internal" href="#cmdoption-zipfile-l"><code>-l</code></a>, <a class="reference internal" href="#cmdoption-zipfile-e"><code>-e</code></a> and <a class="reference internal" href="#cmdoption-zipfile-t"><code>-t</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd> +</dl> </section> </section> <section id="decompression-pitfalls"> <h2>Decompression pitfalls</h2> <p>The extraction in zipfile module might fail due to some pitfalls listed below.</p> <section id="from-file-itself"> <h3>From file itself</h3> <p>Decompression may fail due to incorrect password / CRC checksum / ZIP format or unsupported compression method / decryption.</p> </section> <section id="file-system-limitations"> <h3>File System limitations</h3> <p>Exceeding limitations on different file systems can cause decompression failed. Such as allowable characters in the directory entries, length of the file name, length of the pathname, size of a single file, and number of files, etc.</p> </section> <section id="resources-limitations"> <span id="zipfile-resources-limitations"></span><h3>Resources limitations</h3> <p>The lack of memory or disk volume would lead to decompression failed. For example, decompression bombs (aka <a class="reference external" href="https://en.wikipedia.org/wiki/Zip_bomb">ZIP bomb</a>) apply to zipfile library that can cause disk volume exhaustion.</p> </section> <section id="interruption"> <h3>Interruption</h3> <p>Interruption during the decompression, such as pressing control-C or killing the decompression process may result in incomplete decompression of the archive.</p> </section> <section id="default-behaviors-of-extraction"> <h3>Default behaviors of extraction</h3> <p>Not knowing the default extraction behaviors can cause unexpected decompression results. For example, when extracting the same archive twice, it overwrites files without asking.</p> </section> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/zipfile.html" class="_attribution-link">https://docs.python.org/3.12/library/zipfile.html</a> + </p> +</div> |
