Back to module index
Go to module by name
zipfile
Read and write ZIP files.
XXX references to utf-8 need further investigation.
Classes
BadZipFile
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>
BadZipFile
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>
CompleteDirs
A ZipFile subclass that ensures that implied directories
are always included in the namelist.
close(self)
Close the file, and for mode 'w', 'x' and 'a' write the ending
records.
extract(self, member, path=None, pwd=None)
Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
as possible. `member' may be a filename or a ZipInfo object. You can
specify a different directory using `path'.
extractall(self, 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().
getinfo(self, name)
Return the instance of ZipInfo given 'name'.
infolist(self)
Return a list of class ZipInfo instances for files in the
archive.
make(source)
Given a source (filename or zipfile), return an
appropriate CompleteDirs subclass.
namelist(self)
open(self, name, mode='r', pwd=None, *, force_zip64=False)
Return file-like object for 'name'.
name is a string for the file name within the ZIP file, or a ZipInfo
object.
mode should be 'r' to read a file already in the ZIP file, or 'w' to
write to a file newly added to the archive.
pwd is the password to decrypt files (only used for reading).
When writing, if the file size is not known in advance but may exceed
2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
files. If the size is known in advance, it is best to pass a ZipInfo
instance for name, with zinfo.file_size set.
printdir(self, file=None)
Print a table of contents for the zip file.
read(self, name, pwd=None)
Return file bytes for name.
resolve_dir(self, name)
If the name represents a directory, return that name
as a directory (with the trailing slash).
setpassword(self, pwd)
Set default password for encrypted files.
testzip(self)
Read all the files and check the CRC.
write(self, filename, arcname=None, compress_type=None, compresslevel=None)
Put the bytes from filename into the archive under the name
arcname.
writestr(self, 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.
'zinfo_or_arcname' is either a ZipInfo instance or
the name of the file in the archive.
comment = <property object at 0x7f0226f95f40>
The comment text associated with the ZIP file.
fp = None
FastLookup
ZipFile subclass to ensure implicit
dirs exist and are resolved rapidly.
close(self)
Close the file, and for mode 'w', 'x' and 'a' write the ending
records.
extract(self, member, path=None, pwd=None)
Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
as possible. `member' may be a filename or a ZipInfo object. You can
specify a different directory using `path'.
extractall(self, 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().
getinfo(self, name)
Return the instance of ZipInfo given 'name'.
infolist(self)
Return a list of class ZipInfo instances for files in the
archive.
make(source)
Given a source (filename or zipfile), return an
appropriate CompleteDirs subclass.
namelist(self)
open(self, name, mode='r', pwd=None, *, force_zip64=False)
Return file-like object for 'name'.
name is a string for the file name within the ZIP file, or a ZipInfo
object.
mode should be 'r' to read a file already in the ZIP file, or 'w' to
write to a file newly added to the archive.
pwd is the password to decrypt files (only used for reading).
When writing, if the file size is not known in advance but may exceed
2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
files. If the size is known in advance, it is best to pass a ZipInfo
instance for name, with zinfo.file_size set.
printdir(self, file=None)
Print a table of contents for the zip file.
read(self, name, pwd=None)
Return file bytes for name.
resolve_dir(self, name)
If the name represents a directory, return that name
as a directory (with the trailing slash).
setpassword(self, pwd)
Set default password for encrypted files.
testzip(self)
Read all the files and check the CRC.
write(self, filename, arcname=None, compress_type=None, compresslevel=None)
Put the bytes from filename into the archive under the name
arcname.
writestr(self, 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.
'zinfo_or_arcname' is either a ZipInfo instance or
the name of the file in the archive.
comment = <property object at 0x7f0226f95f40>
The comment text associated with the ZIP file.
fp = None
LZMACompressor
compress(self, data)
flush(self)
LZMADecompressor
decompress(self, data)
LargeZipFile
Raised when writing a zipfile, the zipfile requires ZIP64 extensions
and those extensions are disabled.
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>
Path
A pathlib-compatible interface for zip files.
Consider a zip file with this structure::
.
├── a.txt
└── b
├── c.txt
└── d
└── e.txt
>>> data = io.BytesIO()
>>> zf = ZipFile(data, 'w')
>>> zf.writestr('a.txt', 'content of a')
>>> zf.writestr('b/c.txt', 'content of c')
>>> zf.writestr('b/d/e.txt', 'content of e')
>>> zf.filename = 'abcde.zip'
Path accepts the zipfile object itself or a filename
>>> root = Path(zf)
From there, several path operations are available.
Directory iteration (including the zip file itself):
>>> a, b = root.iterdir()
>>> a
Path('abcde.zip', 'a.txt')
>>> b
Path('abcde.zip', 'b/')
name property:
>>> b.name
'b'
join with divide operator:
>>> c = b / 'c.txt'
>>> c
Path('abcde.zip', 'b/c.txt')
>>> c.name
'c.txt'
Read text:
>>> c.read_text()
'content of c'
existence:
>>> c.exists()
True
>>> (b / 'missing.txt').exists()
False
Coercion to string:
>>> str(c)
'abcde.zip/b/c.txt'
exists(self)
is_dir(self)
is_file(self)
iterdir(self)
joinpath(self, add)
open(self, mode='r', *args, **kwargs)
Open this entry as text or binary following the semantics
of ``pathlib.Path.open()`` by passing arguments through
to io.TextIOWrapper().
read_bytes(self)
read_text(self, *args, **kwargs)
name = <property object at 0x7f0226ed1180>
parent = <property object at 0x7f0226ed11d0>
PyZipFile
Class to create ZIP archives with Python library files and packages.
close(self)
Close the file, and for mode 'w', 'x' and 'a' write the ending
records.
extract(self, member, path=None, pwd=None)
Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
as possible. `member' may be a filename or a ZipInfo object. You can
specify a different directory using `path'.
extractall(self, 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().
getinfo(self, name)
Return the instance of ZipInfo given 'name'.
infolist(self)
Return a list of class ZipInfo instances for files in the
archive.
namelist(self)
Return a list of file names in the archive.
open(self, name, mode='r', pwd=None, *, force_zip64=False)
Return file-like object for 'name'.
name is a string for the file name within the ZIP file, or a ZipInfo
object.
mode should be 'r' to read a file already in the ZIP file, or 'w' to
write to a file newly added to the archive.
pwd is the password to decrypt files (only used for reading).
When writing, if the file size is not known in advance but may exceed
2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
files. If the size is known in advance, it is best to pass a ZipInfo
instance for name, with zinfo.file_size set.
printdir(self, file=None)
Print a table of contents for the zip file.
read(self, name, pwd=None)
Return file bytes for name.
setpassword(self, pwd)
Set default password for encrypted files.
testzip(self)
Read all the files and check the CRC.
write(self, filename, arcname=None, compress_type=None, compresslevel=None)
Put the bytes from filename into the archive under the name
arcname.
writepy(self, pathname, basename='', filterfunc=None)
Add all files from "pathname" to the ZIP archive.
If pathname is a package directory, search the directory and
all package subdirectories recursively for all *.py and enter
the modules into the archive. If pathname is a plain
directory, listdir *.py and enter all modules. Else, pathname
must be a Python *.py file and the module will be put into the
archive. Added modules are always module.pyc.
This method will compile the module.py into module.pyc if
necessary.
If filterfunc(pathname) is given, it is called with every argument.
When it is False, the file or directory is skipped.
writestr(self, 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.
'zinfo_or_arcname' is either a ZipInfo instance or
the name of the file in the archive.
comment = <property object at 0x7f0226f95f40>
The comment text associated with the ZIP file.
fp = None
ZipExtFile
File-like object for reading an archive member.
Is returned by ZipFile.open().
close(self)
detach(self, /)
Disconnect this buffer from its underlying raw stream and return it.
After the raw stream has been detached, the buffer is in an unusable
state.
fileno(self, /)
Returns underlying file descriptor if one exists.
OSError is raised if the IO object does not use a file descriptor.
flush(self, /)
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
isatty(self, /)
Return whether this is an 'interactive' stream.
Return False if it can't be determined.
peek(self, n=1)
Returns buffered bytes without advancing the position.
read(self, n=-1)
Read and return up to n bytes.
If the argument is omitted, None, or negative, data is read and returned until EOF is reached.
read1(self, n)
Read up to n bytes with at most one read() system call.
readable(self)
readinto(self, buffer, /)
readinto1(self, buffer, /)
readline(self, limit=-1)
Read and return a line from the stream.
If limit is specified, at most limit bytes will be read.
readlines(self, hint=-1, /)
Return a list of lines from the stream.
hint can be specified to control the number of lines read: no more
lines will be read if the total size (in bytes/characters) of all
lines so far exceeds hint.
seek(self, offset, whence=0)
seekable(self)
tell(self)
truncate(...)
Truncate file to size bytes.
File pointer is left unchanged. Size defaults to the current IO
position as reported by tell(). Returns the new size.
writable(self, /)
Return whether object was opened for writing.
If False, write() will raise OSError.
write(...)
Write the given buffer to the IO stream.
Returns the number of bytes written, which is always the length of b
in bytes.
Raises BlockingIOError if the buffer is full and the
underlying raw stream cannot accept more data at the moment.
writelines(self, lines, /)
Write a list of lines to stream.
Line separators are not added, so it is usual for each of the
lines provided to have a line separator at the end.
MAX_N = 1073741824
MAX_SEEK_READ = 16777216
MIN_READ_SIZE = 4096
closed = <attribute 'closed' of '_io._IOBase' objects>
ZipFile
Class with methods to open, read, write, close, list zip files.
z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True,
compresslevel=None)
file: Either the path to the file, or a file-like object.
If it is a path, the file will be opened and closed by ZipFile.
mode: The mode can be either read 'r', write 'w', exclusive create 'x',
or append 'a'.
compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma).
allowZip64: if True ZipFile will create files with ZIP64 extensions when
needed, otherwise it will raise an exception when this would
be necessary.
compresslevel: None (default for the given compression type) or an integer
specifying the level to pass to the compressor.
When using ZIP_STORED or ZIP_LZMA this keyword has no effect.
When using ZIP_DEFLATED integers 0 through 9 are accepted.
When using ZIP_BZIP2 integers 1 through 9 are accepted.
close(self)
Close the file, and for mode 'w', 'x' and 'a' write the ending
records.
extract(self, member, path=None, pwd=None)
Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately
as possible. `member' may be a filename or a ZipInfo object. You can
specify a different directory using `path'.
extractall(self, 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().
getinfo(self, name)
Return the instance of ZipInfo given 'name'.
infolist(self)
Return a list of class ZipInfo instances for files in the
archive.
namelist(self)
Return a list of file names in the archive.
open(self, name, mode='r', pwd=None, *, force_zip64=False)
Return file-like object for 'name'.
name is a string for the file name within the ZIP file, or a ZipInfo
object.
mode should be 'r' to read a file already in the ZIP file, or 'w' to
write to a file newly added to the archive.
pwd is the password to decrypt files (only used for reading).
When writing, if the file size is not known in advance but may exceed
2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
files. If the size is known in advance, it is best to pass a ZipInfo
instance for name, with zinfo.file_size set.
printdir(self, file=None)
Print a table of contents for the zip file.
read(self, name, pwd=None)
Return file bytes for name.
setpassword(self, pwd)
Set default password for encrypted files.
testzip(self)
Read all the files and check the CRC.
write(self, filename, arcname=None, compress_type=None, compresslevel=None)
Put the bytes from filename into the archive under the name
arcname.
writestr(self, 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.
'zinfo_or_arcname' is either a ZipInfo instance or
the name of the file in the archive.
comment = <property object at 0x7f0226f95f40>
The comment text associated with the ZIP file.
fp = None
ZipInfo
Class with attributes describing each file in the ZIP archive.
FileHeader(self, zip64=None)
Return the per-file header as a bytes object.
from_file(filename, arcname=None, *, strict_timestamps=True)
Construct an appropriate ZipInfo for a file on the filesystem.
filename should be the path to a file or directory on the filesystem.
arcname is the name which it will have within the archive (by default,
this will be the same as filename, but without a drive letter and with
leading path separators removed).
is_dir(self)
Return True if this archive member is a directory.
CRC = <member 'CRC' of 'ZipInfo' objects>
comment = <member 'comment' of 'ZipInfo' objects>
compress_size = <member 'compress_size' of 'ZipInfo' objects>
compress_type = <member 'compress_type' of 'ZipInfo' objects>
create_system = <member 'create_system' of 'ZipInfo' objects>
create_version = <member 'create_version' of 'ZipInfo' objects>
date_time = <member 'date_time' of 'ZipInfo' objects>
external_attr = <member 'external_attr' of 'ZipInfo' objects>
extra = <member 'extra' of 'ZipInfo' objects>
extract_version = <member 'extract_version' of 'ZipInfo' objects>
file_size = <member 'file_size' of 'ZipInfo' objects>
filename = <member 'filename' of 'ZipInfo' objects>
flag_bits = <member 'flag_bits' of 'ZipInfo' objects>
header_offset = <member 'header_offset' of 'ZipInfo' objects>
internal_attr = <member 'internal_attr' of 'ZipInfo' objects>
orig_filename = <member 'orig_filename' of 'ZipInfo' objects>
reserved = <member 'reserved' of 'ZipInfo' objects>
volume = <member 'volume' of 'ZipInfo' objects>
BadZipFile
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>
Functions
crc32
crc32(data, value=0, /)
Compute a CRC-32 checksum of data.
value
Starting value of the checksum.
The returned checksum is an integer.
is_zipfile
is_zipfile(filename)
Quickly see if a file is a ZIP file by checking the magic number.
The filename argument may be a file or file-like object too.
main
main(args=None)
Other members
BZIP2_VERSION = 46
DEFAULT_VERSION = 20
LZMA_VERSION = 63
MAX_EXTRACT_VERSION = 63
ZIP64_LIMIT = 2147483647
ZIP64_VERSION = 45
ZIP_BZIP2 = 12
ZIP_DEFLATED = 8
ZIP_FILECOUNT_LIMIT = 65535
ZIP_LZMA = 14
ZIP_MAX_COMMENT = 65535
ZIP_STORED = 0
compressor_names = {0: 'store', 1: 'shrink', 2: 'reduce', 3: 'reduce', 4: 'reduce', 5: 'reduce', 6: 'implode', 7: 'tokenize', 8: 'deflate', 9: 'deflate64', 10: 'implode', 12: 'bzip2', 14: 'lzma', 18: 'terse', 19: 'lz77', 97: 'wavpack', 98: 'ppmd'}
sizeCentralDir = 46
sizeEndCentDir = 22
sizeEndCentDir64 = 56
sizeEndCentDir64Locator = 20
sizeFileHeader = 30
stringCentralDir = b'PK\x01\x02'
stringEndArchive = b'PK\x05\x06'
stringEndArchive64 = b'PK\x06\x06'
stringEndArchive64Locator = b'PK\x06\x07'
stringFileHeader = b'PK\x03\x04'
structCentralDir = '<4s4B4HL2L5H2L'
structEndArchive = b'<4s4H2LH'
structEndArchive64 = '<4sQ2H2L4Q'
structEndArchive64Locator = '<4sLQL'
structFileHeader = '<4s2B4HL2L2H'
Modules
binascii
bz2
contextlib
importlib
io
itertools
lzma
os
posixpath
shutil
stat
struct
sys
threading
time
zlib