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 c
| 1227 | |
| 1228 | |
| 1229 | class ZipFile: |
| 1230 | """ Class with methods to open, read, write, close, list zip files. |
| 1231 | |
| 1232 | z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True, |
| 1233 | compresslevel=None) |
| 1234 | |
| 1235 | file: Either the path to the file, or a file-like object. |
| 1236 | If it is a path, the file will be opened and closed by ZipFile. |
| 1237 | mode: The mode can be either read 'r', write 'w', exclusive create 'x', |
| 1238 | or append 'a'. |
| 1239 | compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), |
| 1240 | ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma). |
| 1241 | allowZip64: if True ZipFile will create files with ZIP64 extensions when |
| 1242 | needed, otherwise it will raise an exception when this would |
| 1243 | be necessary. |
| 1244 | compresslevel: None (default for the given compression type) or an integer |
| 1245 | specifying the level to pass to the compressor. |
| 1246 | When using ZIP_STORED or ZIP_LZMA this keyword has no effect. |
| 1247 | When using ZIP_DEFLATED integers 0 through 9 are accepted. |
| 1248 | When using ZIP_BZIP2 integers 1 through 9 are accepted. |
| 1249 | |
| 1250 | """ |
| 1251 | |
| 1252 | fp = None # Set here since __del__ checks it |
| 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 |
no outgoing calls
no test coverage detected