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
| 1382 | |
| 1383 | |
| 1384 | class ZipFile: |
| 1385 | """ Class with methods to open, read, write, close, list zip files. |
| 1386 | |
| 1387 | z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True, |
| 1388 | compresslevel=None) |
| 1389 | |
| 1390 | file: Either the path to the file, or a file-like object. |
| 1391 | If it is a path, the file will be opened and closed by ZipFile. |
| 1392 | mode: The mode can be either read 'r', write 'w', exclusive create 'x', |
| 1393 | or append 'a'. |
| 1394 | compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), |
| 1395 | ZIP_BZIP2 (requires bz2), ZIP_LZMA (requires lzma), or |
| 1396 | ZIP_ZSTANDARD (requires compression.zstd). |
| 1397 | allowZip64: if True ZipFile will create files with ZIP64 extensions when |
| 1398 | needed, otherwise it will raise an exception when this would |
| 1399 | be necessary. |
| 1400 | compresslevel: None (default for the given compression type) or an integer |
| 1401 | specifying the level to pass to the compressor. |
| 1402 | When using ZIP_STORED or ZIP_LZMA this keyword has no effect. |
| 1403 | When using ZIP_DEFLATED integers 0 through 9 are accepted. |
| 1404 | When using ZIP_BZIP2 integers 1 through 9 are accepted. |
| 1405 | When using ZIP_ZSTANDARD integers -7 though 22 are common, |
| 1406 | see the CompressionParameter enum in compression.zstd for |
| 1407 | details. |
| 1408 | |
| 1409 | """ |
| 1410 | |
| 1411 | fp = None # Set here since __del__ checks it |
| 1412 | _windows_illegal_name_trans_table = None |
| 1413 | |
| 1414 | def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True, |
| 1415 | compresslevel=None, *, strict_timestamps=True, metadata_encoding=None): |
| 1416 | """Open the ZIP file with mode read 'r', write 'w', exclusive create 'x', |
| 1417 | or append 'a'.""" |
| 1418 | if mode not in ('r', 'w', 'x', 'a'): |
| 1419 | raise ValueError("ZipFile requires mode 'r', 'w', 'x', or 'a'") |
| 1420 | |
| 1421 | _check_compression(compression) |
| 1422 | |
| 1423 | self._allowZip64 = allowZip64 |
| 1424 | self._didModify = False |
| 1425 | self.debug = 0 # Level of printing: 0 through 3 |
| 1426 | self.NameToInfo = {} # Find file info given name |
| 1427 | self.filelist = [] # List of ZipInfo instances for archive |
| 1428 | self.compression = compression # Method of compression |
| 1429 | self.compresslevel = compresslevel |
| 1430 | self.mode = mode |
| 1431 | self.pwd = None |
| 1432 | self._comment = b'' |
| 1433 | self._strict_timestamps = strict_timestamps |
| 1434 | self.metadata_encoding = metadata_encoding |
| 1435 | |
| 1436 | # Check that we don't try to write with nonconforming codecs |
| 1437 | if self.metadata_encoding and mode != 'r': |
| 1438 | raise ValueError( |
| 1439 | "metadata_encoding is only supported for reading files") |
| 1440 | |
| 1441 | # Check if we were passed a file-like object |
no outgoing calls
searching dependent graphs…