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)
| 1253 | _windows_illegal_name_trans_table = None |
| 1254 | |
| 1255 | def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True, |
| 1256 | compresslevel=None, *, strict_timestamps=True, metadata_encoding=None): |
| 1257 | """Open the ZIP file with mode read 'r', write 'w', exclusive create 'x', |
| 1258 | or append 'a'.""" |
| 1259 | if mode not in ('r', 'w', 'x', 'a'): |
| 1260 | raise ValueError("ZipFile requires mode 'r', 'w', 'x', or 'a'") |
| 1261 | |
| 1262 | _check_compression(compression) |
| 1263 | |
| 1264 | self._allowZip64 = allowZip64 |
| 1265 | self._didModify = False |
| 1266 | self.debug = 0 # Level of printing: 0 through 3 |
| 1267 | self.NameToInfo = {} # Find file info given name |
| 1268 | self.filelist = [] # List of ZipInfo instances for archive |
| 1269 | self.compression = compression # Method of compression |
| 1270 | self.compresslevel = compresslevel |
| 1271 | self.mode = mode |
| 1272 | self.pwd = None |
| 1273 | self._comment = b'' |
| 1274 | self._strict_timestamps = strict_timestamps |
| 1275 | self.metadata_encoding = metadata_encoding |
| 1276 | |
| 1277 | # Check that we don't try to write with nonconforming codecs |
| 1278 | if self.metadata_encoding and mode != 'r': |
| 1279 | raise ValueError( |
| 1280 | "metadata_encoding is only supported for reading files") |
| 1281 | |
| 1282 | # Check if we were passed a file-like object |
| 1283 | if isinstance(file, os.PathLike): |
| 1284 | file = os.fspath(file) |
| 1285 | if isinstance(file, str): |
| 1286 | # No, it's a filename |
| 1287 | self._filePassed = 0 |
| 1288 | self.filename = file |
| 1289 | modeDict = {'r' : 'rb', 'w': 'w+b', 'x': 'x+b', 'a' : 'r+b', |
| 1290 | 'r+b': 'w+b', 'w+b': 'wb', 'x+b': 'xb'} |
| 1291 | filemode = modeDict[mode] |
| 1292 | while True: |
| 1293 | try: |
| 1294 | self.fp = io.open(file, filemode) |
| 1295 | except OSError: |
| 1296 | if filemode in modeDict: |
| 1297 | filemode = modeDict[filemode] |
| 1298 | continue |
| 1299 | raise |
| 1300 | break |
| 1301 | else: |
| 1302 | self._filePassed = 1 |
| 1303 | self.fp = file |
| 1304 | self.filename = getattr(file, 'name', None) |
| 1305 | self._fileRefCnt = 1 |
| 1306 | self._lock = threading.RLock() |
| 1307 | self._seekable = True |
| 1308 | self._writing = False |
| 1309 | |
| 1310 | try: |
| 1311 | if mode == 'r': |
| 1312 | self._RealGetContents() |
nothing calls this directly
no test coverage detected