打开 ZIP 文件,其中
file
can be a path to a file (a string), a file-like object or a
像路径对象
.
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. If
mode
is
'x'
and
file
refers to an existing file, a
FileExistsError
会被引发。若
mode
is
'a'
and
file
refers to an existing ZIP file, then additional files are added to it. If
file
does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file (such as
python.exe
)。若
mode
is
'a'
and the file does not exist at all, it is created. If
mode
is
'r'
or
'a'
, the file should be seekable.
压缩
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. If
ZIP_DEFLATED
,
ZIP_BZIP2
or
ZIP_LZMA
is specified but the corresponding module (
zlib
,
bz2
or
lzma
) is not available,
RuntimeError
is raised. The default is
ZIP_STORED
.
若
allowZip64
is
True
(the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is
false
zipfile
will raise an exception when the ZIP file would require ZIP64 extensions.
The
compresslevel
parameter controls the compression level to use when writing files to the archive. When using
ZIP_STORED
or
ZIP_LZMA
it has no effect. When using
ZIP_DEFLATED
integers
0
through
9
are accepted (see
zlib
for more information). When using
ZIP_BZIP2
integers
1
through
9
are accepted (see
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. Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit.
When mode is
'r'
,
metadata_encoding
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.
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.
ZipFile is also a context manager and therefore supports the
with
statement. In the example,
myzip
被关闭后于
with
语句套件的完成 — 即使出现异常:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
注意
metadata_encoding
is an instance-wide setting for the ZipFile. It is not currently possible to set this on a per-member basis.
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
metadata_encoding
, which is a Python-specific extension.
3.2 版改变:
Added the ability to use
ZipFile
as a context manager.
3.3 版改变:
添加支持
bzip2
and
lzma
压缩。
3.4 版改变:
ZIP64 extensions are enabled by default.
3.5 版改变:
Added support for writing to unseekable streams. Added support for the
'x'
模式。
3.6 版改变:
先前,纯
RuntimeError
was raised for unrecognized compression values.
3.6.2 版改变:
The
file
参数接受
像路径对象
.
3.7 版改变:
添加
compresslevel
参数。
3.8 版改变:
The
strict_timestamps
仅关键词参数。
3.11 版改变:
Added support for specifying member name encoding for reading metadata in the zipfile’s directory and file headers.