zipfileWork with ZIP archives使用ZIP存档

Source code: Lib/zipfile.py


The ZIP file format is a common archive and compression standard. ZIP文件格式是一种常见的存档和压缩标准。This module provides tools to create, read, write, append, and list a ZIP file. 本模块提供了创建、读取、写入、附加和列出ZIP文件的工具。Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.本模块的任何高级使用都需要了解PKZIP应用程序说明中定义的格式。

This module does not currently handle multi-disk ZIP files. 此模块当前不处理多磁盘ZIP文件。It can handle ZIP files that use the ZIP64 extensions (that is ZIP files that are more than 4 GiB in size). 它可以处理使用ZIP64扩展名的ZIP文件(即大小超过4Gib的ZIP文件)。It supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file. 它支持对ZIP存档中的加密文件进行解密,但目前无法创建加密文件。Decryption is extremely slow as it is implemented in native Python rather than C.解密速度非常慢,因为它是在本机Python中实现的,而不是在CTH中。

The module defines the following items:该模块定义了以下各项:

exceptionzipfile.BadZipFile

The error raised for bad ZIP files.错误ZIP文件引发的错误。

New in version 3.2.版本3.2中新增。

exceptionzipfile.BadZipfile

Alias of BadZipFile, for compatibility with older Python versions.BadZipFile的别名,用于与较旧的Python版本兼容。

Deprecated since version 3.2.自版本3.2以来已弃用。

exceptionzipfile.LargeZipFile

The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled.当ZIP文件需要ZIP64功能但尚未启用时,会出现此错误。

classzipfile.ZipFile

The class for reading and writing ZIP files. 用于读取和写入ZIP文件的类。See section ZipFile Objects for constructor details.有关构造函数的详细信息,请参阅ZipFile对象一节。

classzipfile.Path

A pathlib-compatible wrapper for zip files. 与pathlib兼容的zip文件包装器。See section Path Objects for details.有关详细信息,请参阅Path对象部分。

New in version 3.8.版本3.8中新增。

classzipfile.PyZipFile

Class for creating ZIP archives containing Python libraries.用于创建包含Python库的ZIP存档的类。

classzipfile.ZipInfo(filename='NoName', date_time=1980, 1, 1, 0, 0, 0)

Class used to represent information about a member of an archive. 类,用于表示有关存档成员的信息。Instances of this class are returned by the getinfo() and infolist() methods of ZipFile objects. 此类的实例由ZipFile对象的getinfo()infolist()方法返回。Most users of the zipfile module will not need to create these, but only use those created by this module. zipfile模块的大多数用户不需要创建这些文件,而只需要使用此模块创建的文件。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.filename应该是存档成员的全名,date_time应该是一个元组,包含六个字段,这些字段描述了上次修改文件的时间;字段在ZipInfo对象一节中描述。

zipfile.is_zipfile(filename)

Returns True if filename is a valid ZIP file based on its magic number, otherwise returns False. 如果文件名是基于其幻数的有效ZIP文件,则返回True,否则返回Falsefilename may be a file or file-like object too.filename也可以是文件或类似文件的对象。

Changed in version 3.1:版本3.1中更改: 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 zlib module.这需要zlib模块。

zipfile.ZIP_BZIP2

The numeric constant for the BZIP2 compression method. BZIP2压缩方法的数值常数。This requires the bz2 module.这需要bz2模块。

New in version 3.3.版本3.3中新增。

zipfile.ZIP_LZMA

The numeric constant for the LZMA compression method. LZMA压缩方法的数值常数。This requires the lzma module.这需要lzma模块。

New in version 3.3.版本3.3中新增。

Note

The ZIP file format specification has included support for bzip2 compression since 2001, and for LZMA compression since 2006. ZIP文件格式规范自2001年以来一直支持bzip2压缩,自2006年以来一直支持LZMA压缩。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.但是,一些工具(包括较旧的Python版本)不支持这些压缩方法,可能会拒绝完全处理ZIP文件,或者无法提取单个文件。

See also另请参见

PKZIP Application Note应用程序说明

Documentation on the ZIP file format by Phil Katz, the creator of the format and algorithms used.关于ZIP文件格式的文档由Phil Katz编写,他是所用格式和算法的创建者。

Info-ZIP Home Page主页

Information about the Info-ZIP project’s ZIP archive programs and development libraries.有关Info ZIP项目的ZIP存档程序和开发库的信息。

ZipFile Objects对象

classzipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True)

Open a ZIP file, where file can be a path to a file (a string), a file-like object or a path-like object.打开ZIP文件,其中file可以是文件(字符串)的路径、类似文件的对象或类似路径的对象

The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, 'a' to append to an existing file, or 'x' to exclusively create and write a new file. mode参数应为'r'以读取现有文件,'w'以截断并写入新文件,'a'以附加到现有文件,或'x'以独占方式创建并写入新文件。If mode is 'x' and file refers to an existing file, a FileExistsError will be raised. 如果mode'x',并且file引用现有文件,则会引发FileExistsErrorIf mode is 'a' and file refers to an existing ZIP file, then additional files are added to it. 如果mode'a',并且file引用现有的ZIP文件,则会向其添加其他文件。If file does not refer to a ZIP file, then a new ZIP archive is appended to the file. 如果file未引用ZIP文件,则会将新的ZIP存档附加到该文件。This is meant for adding a ZIP archive to another file (such as python.exe). 这是为了向另一个文件(如python.exe)添加ZIP存档。If mode is 'a' and the file does not exist at all, it is created. 如果mode'a',并且文件根本不存在,则会创建该文件。If mode is 'r' or 'a', the file should be seekable.如果mode'r''a',则文件应可查找。

compression is the ZIP compression method to use when writing the archive, and should be ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA; unrecognized values will cause NotImplementedError to be raised. compression是写入归档文件时使用的压缩方法,应该是ZIP_STOREDZIP_DEFLATEDZIP_BZIP2ZIP_LZMA;无法识别的值将导致引发NotImplementedErrorIf ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA is specified but the corresponding module (zlib, bz2 or lzma) is not available, RuntimeError is raised. 如果ZIP_DEFLATEDZIP_BZIP2ZIP_LZMA被指定,但相应的模块(zlibbz2lzma)不可用,则会引发运行时错误。The default is ZIP_STORED.默认值为ZIP_STORED

If allowZip64 is True (the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. 如果allowZip64True(默认),则当zipfile大于4 GiB时,zipfile将创建使用ZIP64扩展名的ZIP文件。If it is false zipfile will raise an exception when the ZIP file would require ZIP64 extensions.如果为falsezipfile将在ZIP文件需要ZIP64扩展名时引发异常。

The compresslevel parameter controls the compression level to use when writing files to the archive. compresslevel参数控制将文件写入归档文件时使用的压缩级别。When using ZIP_STORED or ZIP_LZMA it has no effect. 当使用ZIP_STOREDZIP_LZMA时,它没有任何效果。When using ZIP_DEFLATED integers 0 through 9 are accepted (see zlib for more information). 使用ZIP_DEFLATED整数时,接受09(有关更多信息,请参阅zlib)。When using ZIP_BZIP2 integers 1 through 9 are accepted (see bz2 for more information).使用ZIP_BZIP2时,整数19都是可接受的(有关更多信息,请参阅bz2)。

The strict_timestamps argument, when set to False, allows to zip files older than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. 当设置为False时,strict_timestamps参数允许压缩1980-01-01之前的文件,代价是将时间戳设置为1980-01-01。Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit.在2107-12-31以上的文件中也会出现类似行为,时间戳也设置为限制。

If the file is created with mode 'w', 'x' or 'a' and then closed without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.如果文件是使用模式'w''x''a'创建的,然后closed而不向存档中添加任何文件,则空存档的相应ZIP结构将写入文件。

ZipFile is also a context manager and therefore supports the with statement. ZipFile也是一个上下文管理器,因此支持with语句。In the example, myzip is closed after the with statement’s suite is finished—even if an exception occurs:在本例中,即使出现异常,在with语句的套件完成后,myzip也会关闭:

with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')

New in version 3.2.版本3.2中新增。Added the ability to use ZipFile as a context manager.添加了将ZipFile用作上下文管理器的功能。

Changed in version 3.3:版本3.3中更改: Added support for bzip2 and lzma compression.增加了对bzip2lzma压缩的支持。

Changed in version 3.4:版本3.4中更改: ZIP64 extensions are enabled by default.默认情况下启用ZIP64扩展。

Changed in version 3.5:版本3.5中更改: Added support for writing to unseekable streams. 添加了对写入不可见流的支持。Added support for the 'x' mode.增加了对'x'模式的支持。

Changed in version 3.6:版本3.6中更改: Previously, a plain RuntimeError was raised for unrecognized compression values.以前,对于无法识别的压缩值,会引发普通RuntimeError

Changed in version 3.6.2:版本3.6.2中更改: The file parameter accepts a path-like object.file参数接受类似路径的对象

Changed in version 3.7:版本3.7中更改: Add the compresslevel parameter.添加compresslevel参数。

New in version 3.8.版本3.8中新增。The strict_timestamps keyword-only argumentstrict_timestamps关键字only参数

ZipFile.close()

Close the archive file. 关闭存档文件。You must call close() before exiting your program or essential records will not be written.您必须在退出程序之前调用close(),否则将不会写入基本记录。

ZipFile.getinfo(name)

Return a ZipInfo object with information about the archive member name. 返回一个ZipInfo对象,其中包含有关存档成员name的信息。Calling getinfo() for a name not currently contained in the archive will raise a KeyError.为当前未包含在存档中的名称调用getinfo()将引发KeyError

ZipFile.infolist()

Return a list containing a ZipInfo object for each member of the archive. 返回一个列表,其中包含存档的每个成员的ZipInfo对象。The objects are in the same order as their entries in the actual ZIP file on disk if an existing archive was opened.如果打开了现有存档,则对象的顺序与它们在磁盘上实际ZIP文件中的条目相同。

ZipFile.namelist()

Return a list of archive members by name.按名称返回存档成员列表。

ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)

Access a member of the archive as a binary file-like object. 以类似二进制文件的对象访问存档文件的成员。name can be either the name of a file within the archive or a ZipInfo object. 名称可以是存档中的文件名或ZipInfo对象。The mode parameter, if included, must be 'r' (the default) or 'w'. 模式参数(如果包括)必须为'r'(默认值)或'w'pwd is the password used to decrypt encrypted ZIP files.是用于解密加密ZIP文件的密码。

open() is also a context manager and therefore supports the with statement:也是上下文管理器,因此支持with语句:

with ZipFile('spam.zip') as myzip:
with myzip.open('eggs.txt') as myfile:
print(myfile.read())

With mode 'r' the file-like object (ZipExtFile) is read-only and provides the following methods: read(), readline(), readlines(), readline(), tell(), __iter__(), __next__(). mode 'r'下,类文件对象(ZipExtFile)是只读的,并提供以下方法:read()readline()readlines()readline()tell()__iter__()__next__()These objects can operate independently of the ZipFile.这些对象可以独立于ZipFile进行操作。

With mode='w', a writable file handle is returned, which supports the write() method. mode='w'时,返回一个支持write()方法的可写文件句柄。While a writable file handle is open, attempting to read or write other files in the ZIP file will raise a ValueError.当可写文件句柄打开时,尝试读取或写入ZIP文件中的其他文件将引发ValueError

When writing a file, if the file size is not known in advance but may exceed 2 GiB, pass force_zip64=True to ensure that the header format is capable of supporting large files. 写入文件时,如果文件大小事先未知,但可能超过2 GiB,请传递force_zip64=True,以确保头格式能够支持大型文件。If the file size is known in advance, construct a ZipInfo object with file_size set, and use that as the name parameter.如果预先知道文件大小,则使用file_size构造一个ZipInfo对象,并将其用作名称参数。

Note

The open(), read() and extract() methods can take a filename or a ZipInfo object. open()read()extract()方法可以采用文件名或ZipInfo对象。You will appreciate this when trying to read a ZIP file that contains members with duplicate names.当您试图读取包含具有重复名称的成员的ZIP文件时,您会很感激这一点。

Changed in version 3.6:版本3.6中更改: Removed support of mode='U'. 删除了对mode='U'的支持。Use io.TextIOWrapper for reading compressed text files in universal newlines mode.使用io.TextIOWrapper通用换行模式下读取压缩文本文件。

Changed in version 3.6:版本3.6中更改: open() can now be used to write files into the archive with the mode='w' option.现在可以使用mode='w'选项将文件写入存档。

Changed in version 3.6:版本3.6中更改: Calling open() on a closed ZipFile will raise a ValueError. 对关闭的ZipFile调用open()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

ZipFile.extract(member, path=None, pwd=None)

Extract a member from the archive to the current working directory; member must be its full name or a ZipInfo object. 将存档中的成员提取到当前工作目录;member必须是其全名或ZipInfo对象。Its file information is extracted as accurately as possible. 尽可能准确地提取其文件信息。path specifies a different directory to extract to. path指定要提取到的不同目录。member can be a filename or a ZipInfo object. member可以是文件名或ZipInfo对象。pwd is the password used for encrypted files.pwd是用于加密文件的密码。

Returns the normalized path created (a directory or new file).返回创建的规范化路径(目录或新文件)。

Note

If a member filename is an absolute path, a drive/UNC sharepoint and leading (back)slashes will be stripped, e.g.: ///foo/bar becomes foo/bar on Unix, and C:\foo\bar becomes foo\bar on Windows. 如果成员文件名是绝对路径,则驱动器/UNC sharepoint和前导(后)斜杠将被删除,例如:///foo/bar在Unix上变为foo/bar,而C:\foo\bar在Windows上变为foo\barAnd all ".." components in a member filename will be removed, e.g.: ../../foo../../ba..r becomes foo../ba..r. 以及所有成员文件名中的".."组件将被删除,例如:../../foo../../ba..r变为foo../ba..rOn Windows illegal characters (:, <, >, |, ", ?, and *) replaced by underscore (_).在Windows上,非法字符(:<>|"?*)替换为下划线(_)。

Changed in version 3.6:版本3.6中更改: Calling extract() on a closed ZipFile will raise a ValueError. 对闭合的ZipFile调用extract()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

Changed in version 3.6.2:版本3.6.2中更改: The path parameter accepts a path-like object.path参数接受类似路径的对象

ZipFile.extractall(path=None, members=None, pwd=None)

Extract all members from the archive to the current working directory. 将存档中的所有成员提取到当前工作目录。path specifies a different directory to extract to. 指定要提取到的其他目录。members is optional and must be a subset of the list returned by namelist(). members是可选的,必须是namelist()返回的列表的子集。pwd is the password used for encrypted files.是用于加密文件的密码。

Warning

Never extract archives from untrusted sources without prior inspection. 未经事先检查,不得从不受信任的来源提取档案。It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/" or filenames with two dots "..". 文件可能是在path之外创建的,例如,绝对文件名以"/"开头的成员或文件名以两点".."开头的成员。This module attempts to prevent that. 本模块试图阻止这种情况。See extract() note.请参见extract()注释。

Changed in version 3.6:版本3.6中更改: Calling extractall() on a closed ZipFile will raise a ValueError. 对关闭的ZipFile调用extractall()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

Changed in version 3.6.2:版本3.6.2中更改: The path parameter accepts a path-like object.path参数接受类似路径的对象

ZipFile.printdir()

Print a table of contents for the archive to sys.stdout.将存档的目录打印到sys.stdout

ZipFile.setpassword(pwd)

Set pwd as default password to extract encrypted files.pwd设置为默认密码以提取加密文件。

ZipFile.read(name, pwd=None)

Return the bytes of the file name in the archive. 返回存档中文件name的字节。name is the name of the file in the archive, or a ZipInfo object. The archive must be open for read or append. name是存档中文件的名称,或ZipInfo对象。存档必须打开以供读取或附加。pwd is the password used for encrypted files and, if specified, it will override the default password set with setpassword(). pwd是用于加密文件的密码,如果指定,它将覆盖使用setpassword()设置的默认密码。Calling read() on a ZipFile that uses a compression method other than ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA will raise a NotImplementedError. 对使用压缩方法而不是ZIP_STOREDZIP_DEFLATEDZIP_BZIP2ZIP_LZMAZipFile调用read()将引发NotImplementedErrorAn error will also be raised if the corresponding compression module is not available.如果相应的压缩模块不可用,也会出现错误。

Changed in version 3.6:版本3.6中更改: Calling read() on a closed ZipFile will raise a ValueError. 对关闭的ZipFile调用read()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

ZipFile.testzip()

Read all the files in the archive and check their CRC’s and file headers. 读取存档中的所有文件,并检查其CRC和文件头。Return the name of the first bad file, or else return None.返回第一个None文件的名称,否则不返回任何文件。

Changed in version 3.6:版本3.6中更改: Calling testzip() on a closed ZipFile will raise a ValueError. 在闭合的ZipFile上调用testzip()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

ZipFile.write(filename, arcname=None, compress_type=None, compresslevel=None)

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). 将名为filename的文件写入归档文件,并将其命名为arcname(默认情况下,这将与filename相同,但没有驱动器号,并且删除了前导路径分隔符)。If given, compress_type overrides the value given for the compression parameter to the constructor for the new entry. 如果给定,compress_type会将为compression参数给定的值重写为新条目的构造函数。Similarly, compresslevel will override the constructor if given. 类似地,如果给定,compresslevel将重写构造函数。The archive must be open with mode 'w', 'x' or 'a'.存档必须以模式'w''x''a'打开。

Note

Archive names should be relative to the archive root, that is, they should not start with a path separator.存档名称应相对于存档根,也就是说,它们不应以路径分隔符开头。

Note

If arcname (or filename, if arcname is not given) contains a null byte, the name of the file in the archive will be truncated at the null byte.如果arcname(或filename,如果未给定arcname)包含空字节,则归档文件中的文件名将被截断为空字节。

Note

A leading slash in the filename may lead to the archive being impossible to open in some zip programs on Windows systems.文件名中的前导斜杠可能导致无法在Windows系统上的某些zip程序中打开存档。

Changed in version 3.6:版本3.6中更改: Calling write() on a ZipFile created with mode 'r' or a closed ZipFile will raise a ValueError. 对使用模式'r'创建的ZipFile或闭合的ZipFile调用write()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)

Write a file into the archive. 将文件写入存档。The contents is data, which may be either a str or a bytes instance; if it is a str, it is encoded as UTF-8 first. 内容是data,可以是strbytes实例;如果是str,则首先将其编码为UTF-8。zinfo_or_arcname is either the file name it will be given in the archive, or a ZipInfo instance. zinfo_or_arcname是归档文件中提供的文件名,或者是ZipInfo实例。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 'w', 'x' or 'a'.档案必须以'w''x''a'模式打开。

If given, compress_type overrides the value given for the compression parameter to the constructor for the new entry, or in the zinfo_or_arcname (if that is a ZipInfo instance). 如果给定,compress_type会将为compression参数给定的值覆盖到新条目的构造函数中,或者覆盖在zinfo_or_arcname中(如果是ZipInfo实例)。Similarly, compresslevel will override the constructor if given.类似地,如果给定,compresslevel将重写构造函数。

Note

When passing a ZipInfo instance as the zinfo_or_arcname parameter, the compression method used will be that specified in the compress_type member of the given ZipInfo instance. 当传递ZipInfo实例作为em>zinfo_or_arcname参数时,使用的压缩方法将是在给定ZipInfo实例的compress_type成员中指定的方法。By default, the ZipInfo constructor sets this member to ZIP_STORED.默认情况下,ZipInfo构造函数将此成员设置为ZIP_STORED

Changed in version 3.2:版本3.2中更改: The compress_type argument.compress_type参数。

Changed in version 3.6:版本3.6中更改: Calling writestr() on a ZipFile created with mode 'r' or a closed ZipFile will raise a ValueError. 对使用模式'r'创建的ZipFile或闭合的ZipFile调用writestr()将引发ValueErrorPreviously, a RuntimeError was raised.之前,引发了RuntimeError

The following data attributes are also available:以下数据属性也可用:

ZipFile.filename

Name of the ZIP file.ZIP文件的名称。

ZipFile.debug

The level of debug output to use. 要使用的调试输出级别。This may be set from 0 (the default, no output) to 3 (the most output). 这可以从0(默认值,无输出)设置为3(最大输出)。Debugging information is written to sys.stdout.调试信息会写入sys.stdout

ZipFile.comment

The comment associated with the ZIP file as a bytes object. 作为bytes对象与ZIP文件关联的注释。If assigning a comment to a ZipFile instance created with mode 'w', 'x' or 'a', it should be no longer than 65535 bytes. 如果将注释分配给使用模式'w''x''a'创建的ZipFile实例,则注释长度应不超过65535字节。Comments longer than this will be truncated.超过此长度的注释将被截断。

Path Objects对象

classzipfile.Path(root, at='')

Construct a Path object from a root zipfile (which may be a ZipFile instance or file suitable for passing to the ZipFile constructor).rootzipfile(可能是适合传递给ZipFile构造函数的ZipFile实例或file)构造路径对象。

at specifies the location of this Path within the zipfile, e.g. at指定此路径在zipfile中的位置,例如。‘dir/file.txt’, ‘dir/’, or ‘’. “dir/filetxt”、“dir/”或“”。Defaults to the empty string, indicating the root.默认为空字符串,表示根。

Path objects expose the following features of pathlib.Path objects:路径对象公开了pathlib.Path对象的以下功能:

Path objects are traversable using the / operator or joinpath.路径对象可以使用/运算符或joinpath进行遍历。

Path.name

The final path component.最终路径组件。

Path.open(mode='r', *, pwd, **)

Invoke ZipFile.open() on the current path. 在当前路径上调用ZipFile.open()Allows opening for read or write, text or binary through supported modes: ‘r’, ‘w’, ‘rb’, ‘wb’. 允许通过支持的模式打开读写、文本或二进制文件:“r”、“w”、“rb”、“wb”。Positional and keyword arguments are passed through to io.TextIOWrapper when opened as text and ignored otherwise. 当作为文本打开时,位置和关键字参数传递给io.TextIOWrapper,否则忽略。pwd is the pwd parameter to ZipFile.open().pwdZipFile.open()pwd参数。

Changed in version 3.9:版本3.9中更改: Added support for text and binary modes for open. 增加了对open的文本和二进制模式的支持。Default mode is now text.默认模式现在为文本。

Path.iterdir()

Enumerate the children of the current directory.枚举当前目录的子目录。

Path.is_dir()

Return True if the current context references a directory.如果当前上下文引用目录,则返回True

Path.is_file()

Return True if the current context references a file.如果当前上下文引用文件,则返回True

Path.exists()

Return True if the current context references a file or directory in the zip file.如果当前上下文引用zip文件中的文件或目录,则返回True

Path.read_text(*, **)

Read the current file as unicode text. 将当前文件读取为unicode文本。Positional and keyword arguments are passed through to io.TextIOWrapper (except buffer, which is implied by the context).位置和关键字参数传递给io.TextIOWrapperbuffer除外,缓冲区由上下文暗示)。

Path.read_bytes()

Read the current file as bytes.以字节形式读取当前文件。

Path.joinpath(*other)

Return a new Path object with each of the other arguments joined. 返回一个新的Path对象,并连接每个other参数。The following are equivalent:以下是等效的:

>>> Path(...).joinpath('child').joinpath('grandchild')
>>> Path(...).joinpath('child', 'grandchild')
>>> Path(...) / 'child' / 'grandchild'

Changed in version 3.10:版本3.10中更改: Prior to 3.10, joinpath was undocumented and accepted exactly one parameter.在3.10之前,joinpath没有文档记录,只接受一个参数。

PyZipFile Objects对象

The PyZipFile constructor takes the same parameters as the ZipFile constructor, and one additional parameter, optimize.PyZipFile构造函数采用与ZipFile构造函数相同的参数,还有一个附加参数optimize

classzipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=- 1)

New in version 3.2.版本3.2中新增。The optimize parameter.optimize参数。

Changed in version 3.4:版本3.4中更改: ZIP64 extensions are enabled by default.默认情况下启用ZIP64扩展。

Instances have one method in addition to those of ZipFile objects:实例除了ZipFile对象的方法外,还有一种方法:

writepy(pathname, basename='', filterfunc=None)

Search for files *.py and add the corresponding file to the archive.搜索文件*.py并将相应的文件添加到存档中。

If the optimize parameter to PyZipFile was not given or -1, the corresponding file is a *.pyc file, compiling if necessary.如果PyZipFileoptimize参数没有给定或-1,则相应的文件是一个*.pyc文件,必要时进行编译。

If the optimize parameter to PyZipFile was 0, 1 or 2, only files with that optimization level (see compile()) are added to the archive, compiling if necessary.如果PyZipFileoptimize参数为012,则只有具有该优化级别的文件(请参见compile())才会添加到存档中,并在必要时进行编译。

If pathname is a file, the filename must end with .py, and just the (corresponding *.pyc) file is added at the top level (no path information). 如果pathname是文件,则文件名必须以.py结尾,并且只在顶层添加(对应的*.pyc)文件(无路径信息)。If pathname is a file that does not end with .py, a RuntimeError will be raised. 如果pathname不是以.py结尾的文件,则会引发RuntimeError错误。If it is a directory, and the directory is not a package directory, then all the files *.pyc are added at the top level. 如果它是一个目录,并且该目录不是包目录,那么所有文件*.pyc都添加到顶层。If the directory is a package directory, then all *.pyc 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.如果目录是包目录,则所有*.pyc都添加到包名称下作为文件路径,如果任何子目录是包目录,则所有这些都按排序顺序递归添加。

basename is intended for internal use only.仅供内部使用。

filterfunc, if given, must be a function taking a single string argument. filterfunc(如果给定)必须是一个采用单个字符串参数的函数。It will be passed each path (including each individual full file path) before it is added to the archive. 在将其添加到存档之前,它将被传递到每个路径(包括每个单独的完整文件路径)。If filterfunc returns a false value, the path will not be added, and if it is a directory its contents will be ignored. 如果filterfunc返回假值,则不会添加路径,如果它是目录,则忽略其内容。For example, if our test files are all either in test directories or start with the string test_, we can use a filterfunc to exclude them:例如,如果测试文件都在test目录中或以字符串test_开头,我们可以使用filterfunc来排除它们:

>>> 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)

The writepy() method makes archives with file names like this:writepy()方法使用以下文件名生成存档:

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

New in version 3.4.版本3.4中新增。The filterfunc parameter.filterfunc参数。

Changed in version 3.6.2:版本3.6.2中更改: The pathname parameter accepts a path-like object.pathname参数接受类似路径的对象

Changed in version 3.7:版本3.7中更改: Recursion sorts directory entries.递归对目录项进行排序。

ZipInfo Objects对象

Instances of the ZipInfo class are returned by the getinfo() and infolist() methods of ZipFile objects. ZipInfo类的实例由ZipFile对象的getinfo()infolist()方法返回。Each object stores information about a single member of the ZIP archive.每个对象存储关于ZIP存档的单个成员的信息。

There is one classmethod to make a ZipInfo instance for a filesystem file:有一种类方法可以为文件系统文件创建ZipInfo实例:

classmethodZipInfo.from_file(filename, arcname=None, *, strict_timestamps=True)

Construct a ZipInfo instance for a file on the filesystem, in preparation for adding it to a zip file.为文件系统上的文件构造一个ZipInfo实例,为将其添加到zip文件做准备。

filename should be the path to a file or directory on the filesystem.应该是文件系统上文件或目录的路径。

If arcname is specified, it is used as the name within the archive. 如果指定了arcname,则将其用作存档中的名称。If arcname is not specified, the name will be the same as filename, but with any drive letter and leading path separators removed.如果未指定arcname,则名称将与filename相同,但删除了任何驱动器号和前导路径分隔符。

The strict_timestamps argument, when set to False, allows to zip files older than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. 当设置为False时,strict_timestamps参数允许压缩1980-01-01之前的文件,代价是将时间戳设置为1980-01-01。Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit.在2107-12-31以上的文件中也会出现类似行为,时间戳也设置为限制。

New in version 3.6.版本3.6中新增。

Changed in version 3.6.2:版本3.6.2中更改: The filename parameter accepts a path-like object.filename参数接受类似路径的对象。

New in version 3.8.版本3.8中新增。The strict_timestamps keyword-only argumentstrict_timestamps关键字only参数

Instances have the following methods and attributes:实例具有以下方法和属性:

ZipInfo.is_dir()

Return True if this archive member is a directory.如果此存档成员是目录,则返回True

This uses the entry’s name: directories should always end with /.这使用条目的名称:目录应始终以/结尾。

New in version 3.6.版本3.6中新增。

ZipInfo.filename

Name of the file in the archive.存档中文件的名称。

ZipInfo.date_time

The time and date of the last modification to the archive member. 上次修改存档成员的时间和日期。This is a tuple of six values:这是由六个值组成的元组:

Index索引

Value

0

Year (>= 1980)

1

Month (one-based)月份(从1开始)

2

Day of month (one-based)每月的第几天(从1开始)

3

Hours (zero-based)小时数(从零开始)

4

Minutes (zero-based)分钟(从零开始)

5

Seconds (zero-based)秒(从零开始)

Note

The ZIP file format does not support timestamps before 1980.ZIP文件格式不支持1980年之前的时间戳。

ZipInfo.compress_type

Type of compression for the archive member.存档成员的压缩类型。

ZipInfo.comment

Comment for the individual archive member as a bytes object.作为bytes对象的单个存档成员的注释。

ZipInfo.extra

Expansion field data. 扩展字段数据。The PKZIP Application Note contains some comments on the internal structure of the data contained in this bytes object.PKZIP应用程序说明包含关于此bytes对象中包含的数据的内部结构的一些注释。

ZipInfo.create_system

System which created ZIP archive.创建ZIP存档的系统。

ZipInfo.create_version

PKZIP version which created ZIP archive.创建ZIP存档的PKZIP版本。

ZipInfo.extract_version

PKZIP version needed to extract archive.解压缩存档文件所需的PKZIP版本。

ZipInfo.reserved

Must be zero.必须为零。

ZipInfo.flag_bits

ZIP flag bits.压缩标志位。

ZipInfo.volume

Volume number of file header.文件头的卷号。

ZipInfo.internal_attr

Internal attributes.内部属性。

ZipInfo.external_attr

External file attributes.外部文件属性。

ZipInfo.header_offset

Byte offset to the file header.到文件头的字节偏移量。

ZipInfo.CRC

CRC-32 of the uncompressed file.未压缩文件的CRC-32。

ZipInfo.compress_size

Size of the compressed data.压缩数据的大小。

ZipInfo.file_size

Size of the uncompressed file.未压缩文件的大小。

Command-Line Interface命令行界面

The zipfile module provides a simple command-line interface to interact with ZIP archives.zipfile模块提供了一个简单的命令行界面,用于与ZIP存档进行交互。

If you want to create a new ZIP archive, specify its name after the -c option and then list the filename(s) that should be included:如果要创建新的ZIP存档,请在-c选项后指定其名称,然后列出应包含的文件名:

$ python -m zipfile -c monty.zip spam.txt eggs.txt

Passing a directory is also acceptable:也可以传递目录:

$ python -m zipfile -c monty.zip life-of-brian_1979/

If you want to extract a ZIP archive into the specified directory, use the -e option:如果要将ZIP存档提取到指定目录中,请使用-e选项:

$ python -m zipfile -e monty.zip target-dir/

For a list of the files in a ZIP archive, use the -l option:对于ZIP存档中的文件列表,请使用-l选项:

$ python -m zipfile -l monty.zip

Command-line options命令行选项

-l <zipfile>
--list <zipfile>

List files in a zipfile.在zipfile中列出文件。

-c <zipfile> <source1> ... <sourceN>
--create <zipfile> <source1> ... <sourceN>

Create zipfile from source files.从源文件创建zipfile。

-e <zipfile> <output_dir>
--extract <zipfile> <output_dir>

Extract zipfile into target directory.将zipfile提取到目标目录中。

-t <zipfile>
--test <zipfile>

Test whether the zipfile is valid or not.测试zipfile是否有效。

Decompression pitfalls减压陷阱

The extraction in zipfile module might fail due to some pitfalls listed below.由于以下列出的一些缺陷,zipfile模块中的提取可能会失败。

From file itself从文件本身

Decompression may fail due to incorrect password / CRC checksum / ZIP format or unsupported compression method / decryption.由于密码/CRC校验和/ZIP格式不正确或压缩方法/解密不受支持,解压缩可能会失败。

File System limitations文件系统限制

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.例如目录条目中允许的字符、文件名的长度、路径名的长度、单个文件的大小和文件数等。

Resources limitations资源限制

The lack of memory or disk volume would lead to decompression failed. 内存或磁盘卷不足将导致解压缩失败。For example, decompression bombs (aka ZIP bomb) apply to zipfile library that can cause disk volume exhaustion.例如,解压缩炸弹(也称为ZIP炸弹)应用于zipfile库,可能导致磁盘卷耗尽。

Interruption中断

Interruption during the decompression, such as pressing control-C or killing the decompression process may result in incomplete decompression of the archive.解压缩过程中的中断(如按control-C或终止解压缩过程)可能会导致文件解压缩不完整。

Default behaviors of extraction提取的默认行为

Not knowing the default extraction behaviors can cause unexpected decompression results. 不知道默认提取行为可能会导致意外的解压缩结果。For example, when extracting the same archive twice, it overwrites files without asking.例如,当两次提取同一存档文件时,它会覆盖文件而不需要请求。