Open the ZIP file with mode read 'r', write 'w', exclusive create 'x', or append 'a'.
(self, file, mode="r", compression=ZIP_STORED, allowZip64=True,
compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)
| 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 |
| 1442 | if isinstance(file, os.PathLike): |
| 1443 | file = os.fspath(file) |
| 1444 | if isinstance(file, str): |
| 1445 | # No, it's a filename |
| 1446 | self._filePassed = 0 |
| 1447 | self.filename = file |
| 1448 | modeDict = {'r' : 'rb', 'w': 'w+b', 'x': 'x+b', 'a' : 'r+b', |
| 1449 | 'r+b': 'w+b', 'w+b': 'wb', 'x+b': 'xb'} |
| 1450 | filemode = modeDict[mode] |
| 1451 | while True: |
| 1452 | try: |
| 1453 | self.fp = io.open(file, filemode) |
| 1454 | except OSError: |
| 1455 | if filemode in modeDict: |
| 1456 | filemode = modeDict[filemode] |
| 1457 | continue |
| 1458 | raise |
| 1459 | break |
| 1460 | else: |
| 1461 | self._filePassed = 1 |
| 1462 | self.fp = file |
| 1463 | self.filename = getattr(file, 'name', None) |
| 1464 | self._fileRefCnt = 1 |
| 1465 | self._lock = threading.RLock() |
| 1466 | self._seekable = True |
| 1467 | self._writing = False |
| 1468 | |
| 1469 | try: |
| 1470 | if mode == 'r': |
| 1471 | self._RealGetContents() |
nothing calls this directly
no test coverage detected