1:mod:`zipfile` --- Work with ZIP archives 2========================================= 3 4.. module:: zipfile 5 :synopsis: Read and write ZIP-format archive files. 6 7.. moduleauthor:: James C. Ahlstrom <[email protected]> 8.. sectionauthor:: James C. Ahlstrom <[email protected]> 9 10**Source code:** :source:`Lib/zipfile.py` 11 12-------------- 13 14The ZIP file format is a common archive and compression standard. This module 15provides tools to create, read, write, append, and list a ZIP file. Any 16advanced use of this module will require an understanding of the format, as 17defined in `PKZIP Application Note`_. 18 19This module does not currently handle multi-disk ZIP files. 20It can handle ZIP files that use the ZIP64 extensions 21(that is ZIP files that are more than 4 GiB in size). It supports 22decryption of encrypted files in ZIP archives, but it currently cannot 23create an encrypted file. Decryption is extremely slow as it is 24implemented in native Python rather than C. 25 26The module defines the following items: 27 28.. exception:: BadZipFile 29 30 The error raised for bad ZIP files. 31 32 .. versionadded:: 3.2 33 34 35.. exception:: BadZipfile 36 37 Alias of :exc:`BadZipFile`, for compatibility with older Python versions. 38 39 .. deprecated:: 3.2 40 41 42.. exception:: LargeZipFile 43 44 The error raised when a ZIP file would require ZIP64 functionality but that has 45 not been enabled. 46 47 48.. class:: ZipFile 49 :noindex: 50 51 The class for reading and writing ZIP files. See section 52 :ref:`zipfile-objects` for constructor details. 53 54 55.. class:: Path 56 :noindex: 57 58 Class that implements a subset of the interface provided by 59 :class:`pathlib.Path`, including the full 60 :class:`importlib.resources.abc.Traversable` interface. 61 62 .. versionadded:: 3.8 63 64 65.. class:: PyZipFile 66 :noindex: 67 68 Class for creating ZIP archives containing Python libraries. 69 70 71.. class:: ZipInfo(filename='NoName', date_time=(1980,1,1,0,0,0)) 72 73 Class used to represent information about a member of an archive. Instances 74 of this class are returned by the :meth:`.getinfo` and :meth:`.infolist` 75 methods of :class:`ZipFile` objects. Most users of the :mod:`zipfile` module 76 will not need to create these, but only use those created by this 77 module. *filename* should be the full name of the archive member, and 78 *date_time* should be a tuple containing six fields which describe the time 79 of the last modification to the file; the fields are described in section 80 :ref:`zipinfo-objects`. 81 82.. function:: is_zipfile(filename) 83 84 Returns ``True`` if *filename* is a valid ZIP file based on its magic number, 85 otherwise returns ``False``. *filename* may be a file or file-like object too. 86 87 .. versionchanged:: 3.1 88 Support for file and file-like objects. 89 90 91.. data:: ZIP_STORED 92 93 The numeric constant for an uncompressed archive member. 94 95 96.. data:: ZIP_DEFLATED 97 98 The numeric constant for the usual ZIP compression method. This requires the 99 :mod:`zlib` module. 100 101 102.. data:: ZIP_BZIP2 103 104 The numeric constant for the BZIP2 compression method. This requires the 105 :mod:`bz2` module. 106 107 .. versionadded:: 3.3 108 109.. data:: ZIP_LZMA 110 111 The numeric constant for the LZMA compression method. This requires the 112 :mod:`lzma` module. 113 114 .. versionadded:: 3.3 115 116 .. note:: 117 118 The ZIP file format specification has included support for bzip2 compression 119 since 2001, and for LZMA compression since 2006. However, some tools 120 (including older Python releases) do not support these compression 121 methods, and may either refuse to process the ZIP file altogether, 122 or fail to extract individual files. 123 124 125.. seealso:: 126 127 `PKZIP Application Note`_ 128 Documentation on the ZIP file format by Phil Katz, the creator of the format and 129 algorithms used. 130 131 `Info-ZIP Home Page <https://infozip.sourceforge.net/>`_ 132 Information about the Info-ZIP project's ZIP archive programs and development 133 libraries. 134 135 136.. _zipfile-objects: 137 138ZipFile Objects 139--------------- 140 141 142.. class:: ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, \ 143 compresslevel=None, *, strict_timestamps=True, \ 144 metadata_encoding=None) 145 146 Open a ZIP file, where *file* can be a path to a file (a string), a 147 file-like object or a :term:`path-like object`. 148 149 The *mode* parameter should be ``'r'`` to read an existing 150 file, ``'w'`` to truncate and write a new file, ``'a'`` to append to an 151 existing file, or ``'x'`` to exclusively create and write a new file. 152 If *mode* is ``'x'`` and *file* refers to an existing file, 153 a :exc:`FileExistsError` will be raised. 154 If *mode* is ``'a'`` and *file* refers to an existing ZIP 155 file, then additional files are added to it. If *file* does not refer to a 156 ZIP file, then a new ZIP archive is appended to the file. This is meant for 157 adding a ZIP archive to another file (such as :file:`python.exe`). If 158 *mode* is ``'a'`` and the file does not exist at all, it is created. 159 If *mode* is ``'r'`` or ``'a'``, the file should be seekable. 160 161 *compression* is the ZIP compression method to use when writing the archive, 162 and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, 163 :const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized 164 values will cause :exc:`NotImplementedError` to be raised. If 165 :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified 166 but the corresponding module (:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not 167 available, :exc:`RuntimeError` is raised. The default is :const:`ZIP_STORED`. 168 169 If *allowZip64* is ``True`` (the default) zipfile will create ZIP files that 170 use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is 171 ``false`` :mod:`zipfile` will raise an exception when the ZIP file would 172 require ZIP64 extensions. 173 174 The *compresslevel* parameter controls the compression level to use when 175 writing files to the archive. 176 When using :const:`ZIP_STORED` or :const:`ZIP_LZMA` it has no effect. 177 When using :const:`ZIP_DEFLATED` integers ``0`` through ``9`` are accepted 178 (see :class:`zlib <zlib.compressobj>` for more information). 179 When using :const:`ZIP_BZIP2` integers ``1`` through ``9`` are accepted 180 (see :class:`bz2 <bz2.BZ2File>` for more information). 181 182 The *strict_timestamps* argument, when set to ``False``, allows to 183 zip files older than 1980-01-01 at the cost of setting the 184 timestamp to 1980-01-01. 185 Similar behavior occurs with files newer than 2107-12-31, 186 the timestamp is also set to the limit. 187 188 When mode is ``'r'``, *metadata_encoding* may be set to the name of a codec, 189 which will be used to decode metadata such as the names of members and ZIP 190 comments. 191 192 If the file is created with mode ``'w'``, ``'x'`` or ``'a'`` and then 193 :meth:`closed <close>` without adding any files to the archive, the appropriate 194 ZIP structures for an empty archive will be written to the file. 195 196 ZipFile is also a context manager and therefore supports the 197 :keyword:`with` statement. In the example, *myzip* is closed after the 198 :keyword:`!with` statement's suite is finished---even if an exception occurs:: 199 200 with ZipFile('spam.zip', 'w') as myzip: 201 myzip.write('eggs.txt') 202 203 .. note:: 204 205 *metadata_encoding* is an instance-wide setting for the ZipFile. 206 It is not currently possible to set this on a per-member basis. 207 208 This attribute is a workaround for legacy implementations which produce 209 archives with names in the current locale encoding or code page (mostly 210 on Windows). According to the .ZIP standard, the encoding of metadata 211 may be specified to be either IBM code page (default) or UTF-8 by a flag 212 in the archive header. 213 That flag takes precedence over *metadata_encoding*, which is 214 a Python-specific extension. 215 216 .. versionadded:: 3.2 217 Added the ability to use :class:`ZipFile` as a context manager. 218 219 .. versionchanged:: 3.3 220 Added support for :mod:`bzip2 <bz2>` and :mod:`lzma` compression. 221 222 .. versionchanged:: 3.4 223 ZIP64 extensions are enabled by default. 224 225 .. versionchanged:: 3.5 226 Added support for writing to unseekable streams. 227 Added support for the ``'x'`` mode. 228 229 .. versionchanged:: 3.6 230 Previously, a plain :exc:`RuntimeError` was raised for unrecognized 231 compression values. 232 233 .. versionchanged:: 3.6.2 234 The *file* parameter accepts a :term:`path-like object`. 235 236 .. versionchanged:: 3.7 237 Add the *compresslevel* parameter. 238 239 .. versionadded:: 3.8 240 The *strict_timestamps* keyword-only argument 241 242 .. versionchanged:: 3.11 243 Added support for specifying member name encoding for reading 244 metadata in the zipfile's directory and file headers. 245 246 247.. method:: ZipFile.close() 248 249 Close the archive file. You must call :meth:`close` before exiting your program 250 or essential records will not be written. 251 252 253.. method:: ZipFile.getinfo(name) 254 255 Return a :class:`ZipInfo` object with information about the archive member 256 *name*. Calling :meth:`getinfo` for a name not currently contained in the 257 archive will raise a :exc:`KeyError`. 258 259 260.. method:: ZipFile.infolist() 261 262 Return a list containing a :class:`ZipInfo` object for each member of the 263 archive. The objects are in the same order as their entries in the actual ZIP 264 file on disk if an existing archive was opened. 265 266 267.. method:: ZipFile.namelist() 268 269 Return a list of archive members by name. 270 271 272.. method:: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False) 273 274 Access a member of the archive as a binary file-like object. *name* 275 can be either the name of a file within the archive or a :class:`ZipInfo` 276 object. The *mode* parameter, if included, must be ``'r'`` (the default) 277 or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files as a 278 :class:`bytes` object. 279 280 :meth:`~ZipFile.open` is also a context manager and therefore supports the 281 :keyword:`with` statement:: 282 283 with ZipFile('spam.zip') as myzip: 284 with myzip.open('eggs.txt') as myfile: 285 print(myfile.read()) 286 287 With *mode* ``'r'`` the file-like object 288 (``ZipExtFile``) is read-only and provides the following methods: 289 :meth:`~io.BufferedIOBase.read`, :meth:`~io.IOBase.readline`, 290 :meth:`~io.IOBase.readlines`, :meth:`~io.IOBase.seek`, 291 :meth:`~io.IOBase.tell`, :meth:`~container.__iter__`, :meth:`~iterator.__next__`. 292 These objects can operate independently of the ZipFile. 293 294 With ``mode='w'``, a writable file handle is returned, which supports the 295 :meth:`~io.BufferedIOBase.write` method. While a writable file handle is open, 296 attempting to read or write other files in the ZIP file will raise a 297 :exc:`ValueError`. 298 299 When writing a file, if the file size is not known in advance but may exceed 300 2 GiB, pass ``force_zip64=True`` to ensure that the header format is 301 capable of supporting large files. If the file size is known in advance, 302 construct a :class:`ZipInfo` object with :attr:`~ZipInfo.file_size` set, and 303 use that as the *name* parameter. 304 305 .. note:: 306 307 The :meth:`.open`, :meth:`read` and :meth:`extract` methods can take a filename 308 or a :class:`ZipInfo` object. You will appreciate this when trying to read a 309 ZIP file that contains members with duplicate names. 310 311 .. versionchanged:: 3.6 312 Removed support of ``mode='U'``. Use :class:`io.TextIOWrapper` for reading 313 compressed text files in :term:`universal newlines` mode. 314 315 .. versionchanged:: 3.6 316 :meth:`ZipFile.open` can now be used to write files into the archive with the 317 ``mode='w'`` option. 318 319 .. versionchanged:: 3.6 320 Calling :meth:`.open` on a closed ZipFile will raise a :exc:`ValueError`. 321 Previously, a :exc:`RuntimeError` was raised. 322 323 324.. method:: ZipFile.extract(member, path=None, pwd=None) 325 326 Extract a member from the archive to the current working directory; *member* 327 must be its full name or a :class:`ZipInfo` object. Its file information is 328 extracted as accurately as possible. *path* specifies a different directory 329 to extract to. *member* can be a filename or a :class:`ZipInfo` object. 330 *pwd* is the password used for encrypted files as a :class:`bytes` object. 331 332 Returns the normalized path created (a directory or new file). 333 334 .. note:: 335 336 If a member filename is an absolute path, a drive/UNC sharepoint and 337 leading (back)slashes will be stripped, e.g.: ``///foo/bar`` becomes 338 ``foo/bar`` on Unix, and ``C:\foo\bar`` becomes ``foo\bar`` on Windows. 339 And all ``".."`` components in a member filename will be removed, e.g.: 340 ``../../foo../../ba..r`` becomes ``foo../ba..r``. On Windows illegal 341 characters (``:``, ``<``, ``>``, ``|``, ``"``, ``?``, and ``*``) 342 replaced by underscore (``_``). 343 344 .. versionchanged:: 3.6 345 Calling :meth:`extract` on a closed ZipFile will raise a 346 :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. 347 348 .. versionchanged:: 3.6.2 349 The *path* parameter accepts a :term:`path-like object`. 350 351 352.. method:: ZipFile.extractall(path=None, members=None, pwd=None) 353 354 Extract all members from the archive to the current working directory. *path* 355 specifies a different directory to extract to. *members* is optional and must 356 be a subset of the list returned by :meth:`namelist`. *pwd* is the password 357 used for encrypted files as a :class:`bytes` object. 358 359 .. warning:: 360 361 Never extract archives from untrusted sources without prior inspection. 362 It is possible that files are created outside of *path*, e.g. members 363 that have absolute filenames starting with ``"/"`` or filenames with two 364 dots ``".."``. This module attempts to prevent that. 365 See :meth:`extract` note. 366 367 .. versionchanged:: 3.6 368 Calling :meth:`extractall` on a closed ZipFile will raise a 369 :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. 370 371 .. versionchanged:: 3.6.2 372 The *path* parameter accepts a :term:`path-like object`. 373 374 375.. method:: ZipFile.printdir() 376 377 Print a table of contents for the archive to ``sys.stdout``. 378 379 380.. method:: ZipFile.setpassword(pwd) 381 382 Set *pwd* (a :class:`bytes` object) as default password to extract encrypted files. 383 384 385.. method:: ZipFile.read(name, pwd=None) 386 387 Return the bytes of the file *name* in the archive. *name* is the name of the 388 file in the archive, or a :class:`ZipInfo` object. The archive must be open for 389 read or append. *pwd* is the password used for encrypted files as a :class:`bytes` 390 object and, if specified, overrides the default password set with :meth:`setpassword`. 391 Calling :meth:`read` on a ZipFile that uses a compression method other than 392 :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or 393 :const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also 394 be raised if the corresponding compression module is not available. 395 396 .. versionchanged:: 3.6 397 Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`. 398 Previously, a :exc:`RuntimeError` was raised. 399 400 401.. method:: ZipFile.testzip() 402 403 Read all the files in the archive and check their CRC's and file headers. 404 Return the name of the first bad file, or else return ``None``. 405 406 .. versionchanged:: 3.6 407 Calling :meth:`testzip` on a closed ZipFile will raise a 408 :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. 409 410 411.. method:: ZipFile.write(filename, arcname=None, compress_type=None, \ 412 compresslevel=None) 413 414 Write the file named *filename* to the archive, giving it the archive name 415 *arcname* (by default, this will be the same as *filename*, but without a drive 416 letter and with leading path separators removed). If given, *compress_type* 417 overrides the value given for the *compression* parameter to the constructor for 418 the new entry. Similarly, *compresslevel* will override the constructor if 419 given. 420 The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``. 421 422 .. note:: 423 424 The ZIP file standard historically did not specify a metadata encoding, 425 but strongly recommended CP437 (the original IBM PC encoding) for 426 interoperability. Recent versions allow use of UTF-8 (only). In this 427 module, UTF-8 will automatically be used to write the member names if 428 they contain any non-ASCII characters. It is not possible to write 429 member names in any encoding other than ASCII or UTF-8. 430 431 .. note:: 432 433 Archive names should be relative to the archive root, that is, they should not 434 start with a path separator. 435 436 .. note:: 437 438 If ``arcname`` (or ``filename``, if ``arcname`` is not given) contains a null 439 byte, the name of the file in the archive will be truncated at the null byte. 440 441 .. note:: 442 443 A leading slash in the filename may lead to the archive being impossible to 444 open in some zip programs on Windows systems. 445 446 .. versionchanged:: 3.6 447 Calling :meth:`write` on a ZipFile created with mode ``'r'`` or 448 a closed ZipFile will raise a :exc:`ValueError`. Previously, 449 a :exc:`RuntimeError` was raised. 450 451 452.. method:: ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, \ 453 compresslevel=None) 454 455 Write a file into the archive. The contents is *data*, which may be either 456 a :class:`str` or a :class:`bytes` instance; if it is a :class:`str`, 457 it is encoded as UTF-8 first. *zinfo_or_arcname* is either the file 458 name it will be given in the archive, or a :class:`ZipInfo` instance. If it's 459 an instance, at least the filename, date, and time must be given. If it's a 460 name, the date and time is set to the current date and time. 461 The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'``. 462 463 If given, *compress_type* overrides the value given for the *compression* 464 parameter to the constructor for the new entry, or in the *zinfo_or_arcname* 465 (if that is a :class:`ZipInfo` instance). Similarly, *compresslevel* will 466 override the constructor if given. 467 468 .. note:: 469 470 When passing a :class:`ZipInfo` instance as the *zinfo_or_arcname* parameter, 471 the compression method used will be that specified in the *compress_type* 472 member of the given :class:`ZipInfo` instance. By default, the 473 :class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`. 474 475 .. versionchanged:: 3.2 476 The *compress_type* argument. 477 478 .. versionchanged:: 3.6 479 Calling :meth:`writestr` on a ZipFile created with mode ``'r'`` or 480 a closed ZipFile will raise a :exc:`ValueError`. Previously, 481 a :exc:`RuntimeError` was raised. 482 483.. method:: ZipFile.mkdir(zinfo_or_directory, mode=511) 484 485 Create a directory inside the archive. If *zinfo_or_directory* is a string, 486 a directory is created inside the archive with the mode that is specified in 487 the *mode* argument. If, however, *zinfo_or_directory* is 488 a :class:`ZipInfo` instance then the *mode* argument is ignored. 489 490 The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'``. 491 492 .. versionadded:: 3.11 493 494 495The following data attributes are also available: 496 497.. attribute:: ZipFile.filename 498 499 Name of the ZIP file. 500 501.. attribute:: ZipFile.debug 502 503 The level of debug output to use. This may be set from ``0`` (the default, no 504 output) to ``3`` (the most output). Debugging information is written to 505 ``sys.stdout``. 506 507.. attribute:: ZipFile.comment 508 509 The comment associated with the ZIP file as a :class:`bytes` object. 510 If assigning a comment to a 511 :class:`ZipFile` instance created with mode ``'w'``, ``'x'`` or ``'a'``, 512 it should be no longer than 65535 bytes. Comments longer than this will be 513 truncated. 514 515 516.. _path-objects: 517 518Path Objects 519------------ 520 521.. class:: Path(root, at='') 522 523 Construct a Path object from a ``root`` zipfile (which may be a 524 :class:`ZipFile` instance or ``file`` suitable for passing to 525 the :class:`ZipFile` constructor). 526 527 ``at`` specifies the location of this Path within the zipfile, 528 e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string, 529 indicating the root. 530 531Path objects expose the following features of :mod:`pathlib.Path` 532objects: 533 534Path objects are traversable using the ``/`` operator or ``joinpath``. 535 536.. attribute:: Path.name 537 538 The final path component. 539 540.. method:: Path.open(mode='r', *, pwd, **) 541 542 Invoke :meth:`ZipFile.open` on the current path. 543 Allows opening for read or write, text or binary 544 through supported modes: 'r', 'w', 'rb', 'wb'. 545 Positional and keyword arguments are passed through to 546 :class:`io.TextIOWrapper` when opened as text and 547 ignored otherwise. 548 ``pwd`` is the ``pwd`` parameter to 549 :meth:`ZipFile.open`. 550 551 .. versionchanged:: 3.9 552 Added support for text and binary modes for open. Default 553 mode is now text. 554 555 .. versionchanged:: 3.11.2 556 The ``encoding`` parameter can be supplied as a positional argument 557 without causing a :exc:`TypeError`. As it could in 3.9. Code needing to 558 be compatible with unpatched 3.10 and 3.11 versions must pass all 559 :class:`io.TextIOWrapper` arguments, ``encoding`` included, as keywords. 560 561.. method:: Path.iterdir() 562 563 Enumerate the children of the current directory. 564 565.. method:: Path.is_dir() 566 567 Return ``True`` if the current context references a directory. 568 569.. method:: Path.is_file() 570 571 Return ``True`` if the current context references a file. 572 573.. method:: Path.exists() 574 575 Return ``True`` if the current context references a file or 576 directory in the zip file. 577 578.. data:: Path.suffix 579 580 The file extension of the final component. 581 582 .. versionadded:: 3.11 583 Added :data:`Path.suffix` property. 584 585.. data:: Path.stem 586 587 The final path component, without its suffix. 588 589 .. versionadded:: 3.11 590 Added :data:`Path.stem` property. 591 592.. data:: Path.suffixes 593 594 A list of the path’s file extensions. 595 596 .. versionadded:: 3.11 597 Added :data:`Path.suffixes` property. 598 599.. method:: Path.read_text(*, **) 600 601 Read the current file as unicode text. Positional and 602 keyword arguments are passed through to 603 :class:`io.TextIOWrapper` (except ``buffer``, which is 604 implied by the context). 605 606 .. versionchanged:: 3.11.2 607 The ``encoding`` parameter can be supplied as a positional argument 608 without causing a :exc:`TypeError`. As it could in 3.9. Code needing to 609 be compatible with unpatched 3.10 and 3.11 versions must pass all 610 :class:`io.TextIOWrapper` arguments, ``encoding`` included, as keywords. 611 612.. method:: Path.read_bytes() 613 614 Read the current file as bytes. 615 616.. method:: Path.joinpath(*other) 617 618 Return a new Path object with each of the *other* arguments 619 joined. The following are equivalent:: 620 621 >>> Path(...).joinpath('child').joinpath('grandchild') 622 >>> Path(...).joinpath('child', 'grandchild') 623 >>> Path(...) / 'child' / 'grandchild' 624 625 .. versionchanged:: 3.10 626 Prior to 3.10, ``joinpath`` was undocumented and accepted 627 exactly one parameter. 628 629The `zipp <https://pypi.org/project/zipp>`_ project provides backports 630of the latest path object functionality to older Pythons. Use 631``zipp.Path`` in place of ``zipfile.Path`` for early access to 632changes. 633 634.. _pyzipfile-objects: 635 636PyZipFile Objects 637----------------- 638 639The :class:`PyZipFile` constructor takes the same parameters as the 640:class:`ZipFile` constructor, and one additional parameter, *optimize*. 641 642.. class:: PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, \ 643 optimize=-1) 644 645 .. versionadded:: 3.2 646 The *optimize* parameter. 647 648 .. versionchanged:: 3.4 649 ZIP64 extensions are enabled by default. 650 651 Instances have one method in addition to those of :class:`ZipFile` objects: 652 653 .. method:: PyZipFile.writepy(pathname, basename='', filterfunc=None) 654 655 Search for files :file:`\*.py` and add the corresponding file to the 656 archive. 657 658 If the *optimize* parameter to :class:`PyZipFile` was not given or ``-1``, 659 the corresponding file is a :file:`\*.pyc` file, compiling if necessary. 660 661 If the *optimize* parameter to :class:`PyZipFile` was ``0``, ``1`` or 662 ``2``, only files with that optimization level (see :func:`compile`) are 663 added to the archive, compiling if necessary. 664 665 If *pathname* is a file, the filename must end with :file:`.py`, and 666 just the (corresponding :file:`\*.pyc`) file is added at the top level 667 (no path information). If *pathname* is a file that does not end with 668 :file:`.py`, a :exc:`RuntimeError` will be raised. If it is a directory, 669 and the directory is not a package directory, then all the files 670 :file:`\*.pyc` are added at the top level. If the directory is a 671 package directory, then all :file:`\*.pyc` are added under the package 672 name as a file path, and if any subdirectories are package directories, 673 all of these are added recursively in sorted order. 674 675 *basename* is intended for internal use only. 676 677 *filterfunc*, if given, must be a function taking a single string 678 argument. It will be passed each path (including each individual full 679 file path) before it is added to the archive. If *filterfunc* returns a 680 false value, the path will not be added, and if it is a directory its 681 contents will be ignored. For example, if our test files are all either 682 in ``test`` directories or start with the string ``test_``, we can use a 683 *filterfunc* to exclude them:: 684 685 >>> zf = PyZipFile('myprog.zip') 686 >>> def notests(s): 687 ... fn = os.path.basename(s) 688 ... return (not (fn == 'test' or fn.startswith('test_'))) 689 >>> zf.writepy('myprog', filterfunc=notests) 690 691 The :meth:`writepy` method makes archives with file names like 692 this:: 693 694 string.pyc # Top level name 695 test/__init__.pyc # Package directory 696 test/testall.pyc # Module test.testall 697 test/bogus/__init__.pyc # Subpackage directory 698 test/bogus/myfile.pyc # Submodule test.bogus.myfile 699 700 .. versionadded:: 3.4 701 The *filterfunc* parameter. 702 703 .. versionchanged:: 3.6.2 704 The *pathname* parameter accepts a :term:`path-like object`. 705 706 .. versionchanged:: 3.7 707 Recursion sorts directory entries. 708 709 710.. _zipinfo-objects: 711 712ZipInfo Objects 713--------------- 714 715Instances of the :class:`ZipInfo` class are returned by the :meth:`.getinfo` and 716:meth:`.infolist` methods of :class:`ZipFile` objects. Each object stores 717information about a single member of the ZIP archive. 718 719There is one classmethod to make a :class:`ZipInfo` instance for a filesystem 720file: 721 722.. classmethod:: ZipInfo.from_file(filename, arcname=None, *, \ 723 strict_timestamps=True) 724 725 Construct a :class:`ZipInfo` instance for a file on the filesystem, in 726 preparation for adding it to a zip file. 727 728 *filename* should be the path to a file or directory on the filesystem. 729 730 If *arcname* is specified, it is used as the name within the archive. 731 If *arcname* is not specified, the name will be the same as *filename*, but 732 with any drive letter and leading path separators removed. 733 734 The *strict_timestamps* argument, when set to ``False``, allows to 735 zip files older than 1980-01-01 at the cost of setting the 736 timestamp to 1980-01-01. 737 Similar behavior occurs with files newer than 2107-12-31, 738 the timestamp is also set to the limit. 739 740 .. versionadded:: 3.6 741 742 .. versionchanged:: 3.6.2 743 The *filename* parameter accepts a :term:`path-like object`. 744 745 .. versionadded:: 3.8 746 The *strict_timestamps* keyword-only argument 747 748 749Instances have the following methods and attributes: 750 751.. method:: ZipInfo.is_dir() 752 753 Return ``True`` if this archive member is a directory. 754 755 This uses the entry's name: directories should always end with ``/``. 756 757 .. versionadded:: 3.6 758 759 760.. attribute:: ZipInfo.filename 761 762 Name of the file in the archive. 763 764 765.. attribute:: ZipInfo.date_time 766 767 The time and date of the last modification to the archive member. This is a 768 tuple of six values: 769 770 +-------+--------------------------+ 771 | Index | Value | 772 +=======+==========================+ 773 | ``0`` | Year (>= 1980) | 774 +-------+--------------------------+ 775 | ``1`` | Month (one-based) | 776 +-------+--------------------------+ 777 | ``2`` | Day of month (one-based) | 778 +-------+--------------------------+ 779 | ``3`` | Hours (zero-based) | 780 +-------+--------------------------+ 781 | ``4`` | Minutes (zero-based) | 782 +-------+--------------------------+ 783 | ``5`` | Seconds (zero-based) | 784 +-------+--------------------------+ 785 786 .. note:: 787 788 The ZIP file format does not support timestamps before 1980. 789 790 791.. attribute:: ZipInfo.compress_type 792 793 Type of compression for the archive member. 794 795 796.. attribute:: ZipInfo.comment 797 798 Comment for the individual archive member as a :class:`bytes` object. 799 800 801.. attribute:: ZipInfo.extra 802 803 Expansion field data. The `PKZIP Application Note`_ contains 804 some comments on the internal structure of the data contained in this 805 :class:`bytes` object. 806 807 808.. attribute:: ZipInfo.create_system 809 810 System which created ZIP archive. 811 812 813.. attribute:: ZipInfo.create_version 814 815 PKZIP version which created ZIP archive. 816 817 818.. attribute:: ZipInfo.extract_version 819 820 PKZIP version needed to extract archive. 821 822 823.. attribute:: ZipInfo.reserved 824 825 Must be zero. 826 827 828.. attribute:: ZipInfo.flag_bits 829 830 ZIP flag bits. 831 832 833.. attribute:: ZipInfo.volume 834 835 Volume number of file header. 836 837 838.. attribute:: ZipInfo.internal_attr 839 840 Internal attributes. 841 842 843.. attribute:: ZipInfo.external_attr 844 845 External file attributes. 846 847 848.. attribute:: ZipInfo.header_offset 849 850 Byte offset to the file header. 851 852 853.. attribute:: ZipInfo.CRC 854 855 CRC-32 of the uncompressed file. 856 857 858.. attribute:: ZipInfo.compress_size 859 860 Size of the compressed data. 861 862 863.. attribute:: ZipInfo.file_size 864 865 Size of the uncompressed file. 866 867 868.. _zipfile-commandline: 869.. program:: zipfile 870 871Command-Line Interface 872---------------------- 873 874The :mod:`zipfile` module provides a simple command-line interface to interact 875with ZIP archives. 876 877If you want to create a new ZIP archive, specify its name after the :option:`-c` 878option and then list the filename(s) that should be included: 879 880.. code-block:: shell-session 881 882 $ python -m zipfile -c monty.zip spam.txt eggs.txt 883 884Passing a directory is also acceptable: 885 886.. code-block:: shell-session 887 888 $ python -m zipfile -c monty.zip life-of-brian_1979/ 889 890If you want to extract a ZIP archive into the specified directory, use 891the :option:`-e` option: 892 893.. code-block:: shell-session 894 895 $ python -m zipfile -e monty.zip target-dir/ 896 897For a list of the files in a ZIP archive, use the :option:`-l` option: 898 899.. code-block:: shell-session 900 901 $ python -m zipfile -l monty.zip 902 903 904Command-line options 905~~~~~~~~~~~~~~~~~~~~ 906 907.. cmdoption:: -l <zipfile> 908 --list <zipfile> 909 910 List files in a zipfile. 911 912.. cmdoption:: -c <zipfile> <source1> ... <sourceN> 913 --create <zipfile> <source1> ... <sourceN> 914 915 Create zipfile from source files. 916 917.. cmdoption:: -e <zipfile> <output_dir> 918 --extract <zipfile> <output_dir> 919 920 Extract zipfile into target directory. 921 922.. cmdoption:: -t <zipfile> 923 --test <zipfile> 924 925 Test whether the zipfile is valid or not. 926 927.. cmdoption:: --metadata-encoding <encoding> 928 929 Specify encoding of member names for :option:`-l`, :option:`-e` and 930 :option:`-t`. 931 932 .. versionadded:: 3.11 933 934 935Decompression pitfalls 936---------------------- 937 938The extraction in zipfile module might fail due to some pitfalls listed below. 939 940From file itself 941~~~~~~~~~~~~~~~~ 942 943Decompression may fail due to incorrect password / CRC checksum / ZIP format or 944unsupported compression method / decryption. 945 946File System limitations 947~~~~~~~~~~~~~~~~~~~~~~~ 948 949Exceeding limitations on different file systems can cause decompression failed. 950Such as allowable characters in the directory entries, length of the file name, 951length of the pathname, size of a single file, and number of files, etc. 952 953.. _zipfile-resources-limitations: 954 955Resources limitations 956~~~~~~~~~~~~~~~~~~~~~ 957 958The lack of memory or disk volume would lead to decompression 959failed. For example, decompression bombs (aka `ZIP bomb`_) 960apply to zipfile library that can cause disk volume exhaustion. 961 962Interruption 963~~~~~~~~~~~~ 964 965Interruption during the decompression, such as pressing control-C or killing the 966decompression process may result in incomplete decompression of the archive. 967 968Default behaviors of extraction 969~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 970 971Not knowing the default extraction behaviors 972can cause unexpected decompression results. 973For example, when extracting the same archive twice, 974it overwrites files without asking. 975 976 977.. _ZIP bomb: https://en.wikipedia.org/wiki/Zip_bomb 978.. _PKZIP Application Note: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT 979