def _set_alternative_decompressor(self, coders: List[Dict[str, Any]]) -> None: filter_id = self.alt_methods_map.get(coders[0]['method'], None) if filter_id == self.FILTER_BZIP2: self.decompressor = bz2.BZ2Decompressor() elif filter_id == self.FILTER_COPY: self.decompressor = CopyDecompressor() elif filter_id == self.FILTER_AES: password = ArchivePassword().get() properties = coders[0].get('properties', None) self.decompressor = AESDecompressor(properties, password, coders[1:]) else: raise UnsupportedCompressionMethodError
def __init__(self, file: Union[BinaryIO, str, pathlib.Path], mode: str = 'r', *, filters: Optional[str] = None, password: Optional[str] = None) -> None: if mode not in ('r', 'w', 'x', 'a'): raise ValueError("ZipFile requires mode 'r', 'w', 'x', or 'a'") if password is not None: if mode not in ('r'): raise NotImplementedError( "It has not been implemented to create archive with password." ) ArchivePassword(password) self.password_protected = True else: self.password_protected = False # Check if we were passed a file-like object or not if isinstance(file, str): self._filePassed = False # type: bool self.filename = file # type: str if mode == 'r': self.fp = open(file, 'rb') # type: BinaryIO elif mode == 'w': self.fp = open(file, 'w+b') elif mode == 'x': self.fp = open(file, 'x+b') elif mode == 'a': self.fp = open(file, 'r+b') else: raise ValueError("File open error.") self.mode = mode elif isinstance(file, pathlib.Path): self._filePassed = False self.filename = str(file) if mode == 'r': self.fp = file.open( mode='rb') # type: ignore # noqa # typeshed issue: 2911 elif mode == 'w': self.fp = file.open(mode='w+b') # type: ignore # noqa elif mode == 'x': self.fp = file.open(mode='x+b') # type: ignore # noqa elif mode == 'a': self.fp = file.open(mode='r+b') # type: ignore # noqa else: raise ValueError("File open error.") self.mode = mode elif isinstance(file, io.IOBase): self._filePassed = True self.fp = file self.filename = getattr(file, 'name', None) self.mode = mode # type: ignore #noqa else: raise TypeError("invalid file: {}".format(type(file))) self._fileRefCnt = 1 try: if mode == "r": self._real_get_contents(self.fp) self._reset_worker() elif mode in 'w': # FIXME: check filters here self.folder = self._create_folder(filters) self.files = ArchiveFileList() self._prepare_write() self._reset_worker() elif mode in 'x': raise NotImplementedError elif mode == 'a': raise NotImplementedError else: raise ValueError("Mode must be 'r', 'w', 'x', or 'a'") except Exception as e: self._fpclose() raise e self.encoded_header_mode = False