zipfile
— Work 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:该模块定义了以下各项:
-
exception
zipfile.
BadZipFile
¶ The error raised for bad ZIP files.错误ZIP文件引发的错误。New in version 3.2.版本3.2中新增。
-
exception
zipfile.
BadZipfile
¶ Alias ofBadZipFile
, for compatibility with older Python versions.BadZipFile
的别名,用于与较旧的Python版本兼容。Deprecated since version 3.2.自版本3.2以来已弃用。
-
exception
zipfile.
LargeZipFile
¶ The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled.当ZIP文件需要ZIP64功能但尚未启用时,会出现此错误。
-
class
zipfile.
ZipFile
The class for reading and writing ZIP files.用于读取和写入ZIP文件的类。See section ZipFile Objects for constructor details.有关构造函数的详细信息,请参阅ZipFile
对象一节。
-
class
zipfile.
Path
A pathlib-compatible wrapper for zip files.与pathlib兼容的zip文件包装器。See section Path Objects for details.有关详细信息,请参阅Path
对象部分。New in version 3.8.版本3.8中新增。
-
class
zipfile.
PyZipFile
Class for creating ZIP archives containing Python libraries.用于创建包含Python库的ZIP存档的类。
-
class
zipfile.
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此类的实例由ZipFile对象的getinfo()
andinfolist()
methods ofZipFile
objects.getinfo()
和infolist()
方法返回。Most users of thezipfile
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如果文件名是基于其幻数的有效ZIP文件,则返回True
if filename is a valid ZIP file based on its magic number, otherwise returnsFalse
.True
,否则返回False
。filename 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对象¶
-
class
zipfile.
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 bemode参数应为'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.'r'
以读取现有文件,'w'
以截断并写入新文件,'a'
以附加到现有文件,或'x'
以独占方式创建并写入新文件。If mode is如果mode为'x'
and file refers to an existing file, aFileExistsError
will be raised.'x'
,并且file引用现有文件,则会引发FileExistsError
。If mode is如果mode为'a'
and file refers to an existing ZIP file, then additional files are added to it.'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如果mode为'a'
and the file does not exist at all, it is created.'a'
,并且文件根本不存在,则会创建该文件。If mode is如果mode为'r'
or'a'
, the file should be seekable.'r'
或'a'
,则文件应可查找。compression is the ZIP compression method to use when writing the archive, and should becompression是写入归档文件时使用的压缩方法,应该是ZIP_STORED
,ZIP_DEFLATED
,ZIP_BZIP2
orZIP_LZMA
; unrecognized values will causeNotImplementedError
to be raised.ZIP_STORED
、ZIP_DEFLATED
、ZIP_BZIP2
或ZIP_LZMA
;无法识别的值将导致引发NotImplementedError
。If如果ZIP_DEFLATED
,ZIP_BZIP2
orZIP_LZMA
is specified but the corresponding module (zlib
,bz2
orlzma
) is not available,RuntimeError
is raised.ZIP_DEFLATED
、ZIP_BZIP2
或ZIP_LZMA
被指定,但相应的模块(zlib
、bz2
或lzma
)不可用,则会引发运行时错误。The default is默认值为ZIP_STORED
.ZIP_STORED
。If allowZip64 is如果allowZip64为True
(the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB.True
(默认),则当zipfile大于4 GiB时,zipfile将创建使用ZIP64扩展名的ZIP文件。If it is如果为false
zipfile
will raise an exception when the ZIP file would require ZIP64 extensions.false
,zipfile
将在ZIP文件需要ZIP64扩展名时引发异常。The compresslevel parameter controls the compression level to use when writing files to the archive.compresslevel参数控制将文件写入归档文件时使用的压缩级别。When using当使用ZIP_STORED
orZIP_LZMA
it has no effect.ZIP_STORED
或ZIP_LZMA
时,它没有任何效果。When using使用ZIP_DEFLATED
integers0
through9
are accepted (seezlib
for more information).ZIP_DEFLATED
整数时,接受0
到9
(有关更多信息,请参阅zlib
)。When using使用ZIP_BZIP2
integers1
through9
are accepted (seebz2
for more information).ZIP_BZIP2
时,整数1
到9
都是可接受的(有关更多信息,请参阅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 thenclosed
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 thewith
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.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 aKeyError
.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'
。pwdis 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在mode'r'
the file-like object (ZipExtFile
) is read-only and provides the following methods:read()
,readline()
,readlines()
,readline()
,tell()
,__iter__()
,__next__()
.'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 thewrite()
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当可写文件句柄打开时,尝试读取或写入ZIP文件中的其他文件将引发ValueError
.ValueError
。When writing a file, if the file size is not known in advance but may exceed 2 GiB, pass写入文件时,如果文件大小事先未知,但可能超过2 GiB,请传递force_zip64=True
to ensure that the header format is capable of supporting large files.force_zip64=True
,以确保头格式能够支持大型文件。If the file size is known in advance, construct a如果预先知道文件大小,则使用ZipInfo
object withfile_size
set, and use that as the name parameter.file_size
构造一个ZipInfo
对象,并将其用作名称参数。Note
Theopen()
,read()
andextract()
methods can take a filename or aZipInfo
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对关闭的ZipFile调用open()
on a closed ZipFile will raise aValueError
.open()
将引发ValueError
。Previously, 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将存档中的成员提取到当前工作目录;member必须是其全名或ZipInfo
object.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 amember可以是文件名或ZipInfo
object.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.:如果成员文件名是绝对路径,则驱动器/UNC sharepoint和前导(后)斜杠将被删除,例如:///foo/bar
becomesfoo/bar
on Unix, andC:\foo\bar
becomesfoo\bar
on Windows.///foo/bar
在Unix上变为foo/bar
,而C:\foo\bar
在Windows上变为foo\bar
。And all以及所有成员文件名中的".."
components in a member filename will be removed, e.g.:../../foo../../ba..r
becomesfoo../ba..r
.".."
组件将被删除,例如:../../foo../../ba..r
变为foo../ba..r
。On Windows illegal characters (在Windows上,非法字符(:
,<
,>
,|
,"
,?
, and*
) replaced by underscore (_
).:
、<
、>
、|
、"
、?
和*
)替换为下划线(_
)。Changed in version 3.6:版本3.6中更改:Calling对闭合的extract()
on a closed ZipFile will raise aValueError
.ZipFile
调用extract()
将引发ValueError
。Previously, 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.将存档中的所有成员提取到当前工作目录。pathspecifies a different directory to extract to.指定要提取到的其他目录。members is optional and must be a subset of the list returned bymembers是可选的,必须是namelist()
.namelist()
返回的列表的子集。pwdis 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文件可能是在path之外创建的,例如,绝对文件名以"/"
or filenames with two dots".."
."/"
开头的成员或文件名以两点".."
开头的成员。This module attempts to prevent that.本模块试图阻止这种情况。See请参见extract()
note.extract()
注释。Changed in version 3.6:版本3.6中更改:Calling对关闭的ZipFile调用extractall()
on a closed ZipFile will raise aValueError
.extractall()
将引发ValueError
。Previously, 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 aname是存档中文件的名称,或ZipInfo
object. The archive must be open for read or append.ZipInfo
对象。存档必须打开以供读取或附加。pwd is the password used for encrypted files and, if specified, it will override the default password set withpwd是用于加密文件的密码,如果指定,它将覆盖使用setpassword()
.setpassword()
设置的默认密码。Calling对使用压缩方法而不是read()
on a ZipFile that uses a compression method other thanZIP_STORED
,ZIP_DEFLATED
,ZIP_BZIP2
orZIP_LZMA
will raise aNotImplementedError
.ZIP_STORED
、ZIP_DEFLATED
、ZIP_BZIP2
或ZIP_LZMA
的ZipFile
调用read()
将引发NotImplementedError
。An 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 aValueError
.ZipFile
调用read()
将引发ValueError
。Previously, 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 aValueError
.ZipFile
上调用testzip()
将引发ValueError
。Previously, 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
(orfilename
, ifarcname
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 aValueError
.'r'
创建的ZipFile
或闭合的ZipFile
调用write()
将引发ValueError
。Previously, 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内容是data,可以是str
or abytes
instance; if it is astr
, it is encoded as UTF-8 first.str
或bytes
实例;如果是str
,则首先将其编码为UTF-8。zinfo_or_arcname is either the file name it will be given in the archive, or azinfo_or_arcname是归档文件中提供的文件名,或者是ZipInfo
instance.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如果给定,compress_type会将为compression参数给定的值覆盖到新条目的构造函数中,或者覆盖在zinfo_or_arcname中(如果是ZipInfo
instance).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 givenZipInfo
instance.ZipInfo
实例作为em>zinfo_or_arcname参数时,使用的压缩方法将是在给定ZipInfo
实例的compress_type成员中指定的方法。By default, the默认情况下,ZipInfo
constructor sets this member toZIP_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 aValueError
.'r'
创建的ZipFile
或闭合的ZipFile
调用writestr()
将引发ValueError
。Previously, 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) to3
(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对象¶
-
class
zipfile.
Path
(root, at='')¶ Construct a Path object from a从root
zipfile (which may be aZipFile
instance orfile
suitable for passing to theZipFile
constructor).root
zipfile(可能是适合传递给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 thepwd
parameter toZipFile.open()
.pwd
是ZipFile.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如果当前上下文引用zip文件中的文件或目录,则返回True
if the current context references a file or directory in the zip file.True
。
-
Path.
read_text
(*, **)¶ Read the current file as unicode text.将当前文件读取为unicode文本。Positional and keyword arguments are passed through to位置和关键字参数传递给io.TextIOWrapper
(exceptbuffer
, which is implied by the context).io.TextIOWrapper
(buffer
除外,缓冲区由上下文暗示)。
-
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,在3.10之前,joinpath
was undocumented and accepted exactly one parameter.joinpath
没有文档记录,只接受一个参数。
PyZipFile
Objects对象¶
The PyZipFile
constructor takes the same parameters as the ZipFile
constructor, and one additional parameter, optimize.PyZipFile
构造函数采用与ZipFile
构造函数相同的参数,还有一个附加参数optimize。
-
class
zipfile.
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.PyZipFile
的optimize参数没有给定或-1
,则相应的文件是一个*.pyc
文件,必要时进行编译。If the optimize parameter to如果PyZipFile
was0
,1
or2
, only files with that optimization level (seecompile()
) are added to the archive, compiling if necessary.PyZipFile
的optimize参数为0
、1
或2
,则只有具有该优化级别的文件(请参见compile()
)才会添加到存档中,并在必要时进行编译。If pathname is a file, the filename must end with如果pathname是文件,则文件名必须以.py
, and just the (corresponding*.pyc
) file is added at the top level (no path information)..py
结尾,并且只在顶层添加(对应的*.pyc
)文件(无路径信息)。If pathname is a file that does not end with如果pathname不是以.py
, aRuntimeError
will be raised..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 stringtest_
, 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)Thewritepy()
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.myfileNew 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
实例:
-
classmethod
ZipInfo.
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.
extra
¶ Expansion field data.扩展字段数据。The PKZIP Application Note contains some comments on the internal structure of the data contained in thisPKZIP应用程序说明包含关于此bytes
object.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 如果要创建新的ZIP存档,请在-c
option and then list the filename(s) that should be included:-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 如果要将ZIP存档提取到指定目录中,请使用-e
option:-e
选项:
$ python -m zipfile -e monty.zip target-dir/
For a list of the files in a ZIP archive, use the 对于ZIP存档中的文件列表,请使用-l选项:-l
option:
$ python -m zipfile -l monty.zip
Command-line options命令行选项¶
-
-c
<zipfile> <source1> ... <sourceN>
¶ -
--create
<zipfile> <source1> ... <sourceN>
¶ Create zipfile from source files.从源文件创建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.例如,当两次提取同一存档文件时,它会覆盖文件而不需要请求。