PyDocs

Nav

Data Compression and Archiving

ZipFile

zipfile

class (source)
  • ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)
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), ZIP_LZMA (requires lzma), or
      ZIP_ZSTANDARD (requires compression.zstd).
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.
      When using ZIP_ZSTANDARD integers -7 though 22 are common,
      see the CompressionParameter enum in compression.zstd for
      details.
Changed in 3.2, 3.3, 3.4, 3.5, 3.6, 3.6.2, 3.7, 3.8, 3.11
  • 3.2: Added the ability to use ZipFile as a context manager.
  • 3.3: Added support for bzip2 and lzma compression.
  • 3.4: ZIP64 extensions are enabled by default.
  • 3.5: Added support for writing to unseekable streams. Added support for the 'x' mode.
  • 3.6: Previously, a plain RuntimeError was raised for unrecognized compression values.
  • 3.6.2: The file parameter accepts a path-like object.
  • 3.7: Add the compresslevel parameter.
  • 3.8: The strict_timestamps keyword-only parameter.
  • 3.11: Added support for specifying member name encoding for reading metadata in the zipfile’s directory and file headers.
Members of ZipFile

c

close
Close the file, and for mode 'w', 'x' and 'a' write the ending records.
comment
The comment text associated with the ZIP file.

d

debug
The level of debug output to use.

e

extract
Extract a member from the archive to the current working directory, using its full name.
extractall
Extract all members from the archive to the current working directory.

f

filename
Name of the ZIP file.

g

getinfo
Return the instance of ZipInfo given 'name'.

i

infolist
Return a list of class ZipInfo instances for files in the archive.

m

mkdir
Creates a directory inside the zip archive.

n

namelist
Return a list of file names in the archive.

o

open
Return file-like object for 'name'.

p

printdir
Print a table of contents for the zip file.

r

read
Return file bytes for name.

s

setpassword
Set default password for encrypted files.

t

testzip
Read all the files and check the CRC.

w

write
Put the bytes from filename into the archive under the name arcname.
writestr
Write a file into the archive.
0 Examples
No examples for zipfile/ZipFile.
See Also
No see-alsos for zipfile/ZipFile
0 Notes
No notes for ZipFile