def extract_no_password(zipfile_path: str, extract_path: str) -> bool: """ 尝试用空密码解压\n :param zipfile_path: 压缩文件路径字符串 :param extract_path: 解压路径字符串 :return: 是否能解压成功 """ if zipfile_path.lower().endswith(".zip"): with ZipFile(zipfile_path) as file: try: file.extractall(path=extract_path, members=None, pwd=None) except RuntimeError: return False except BadZipFile: return False except error: return False except Exception as e: print("遇到未知错误", type(e), e) else: return True elif zipfile_path.lower().endswith(".rar"): with RarFile(zipfile_path) as file: try: file.extractall(path=extract_path, members=None, pwd=None) except RuntimeError: return False except BadZipFile: return False except error: return False except Exception as e: print("遇到未知错误", type(e), e) else: return True
def extract_rar(passwords: (str, ), zipfile_path: str, extract_path: str) -> str: """ 解压rar文件\n :param passwords: 密码组成的元组 :param zipfile_path: 压缩文件路径字符串 :param extract_path: 解压路径字符串 :return: 如果找到了密码,返回密码字符串;如果没找到密码,返回None """ with RarFile(zipfile_path) as file: for password in passwords: # 如果是空密码则跳过 if len(password) == 0: continue try: file.extractall(path=extract_path, members=None, pwd=password) except RuntimeError: print("尝试密码 %s 失败" % password) except BadRarFile: print("尝试密码 %s 失败" % password) except Exception as e: print("尝试密码", password, "遇到未知错误", type(e), e) else: print("尝试密码 %s 成功" % password) return password
def __init__(self, file, mode=None): """ :param file: 要处理的文件 :param mode: 要处理的格式,不输入会有一套智能匹配算法 'rar': 'zip': docx后缀的,默认采用zip格式解压 """ # 1、确定压缩格式 name, ext = os.path.splitext(file) ext = ext.lower() if not mode: if ext in ('.docx', '.zip'): mode = 'zip' elif ext == '.rar': mode = 'rar' else: dprint(ext) # 从文件扩展名无法得知压缩格式 raise ValueError self.mode = mode # 2、确定是用的解压“引擎” if mode == 'zip': self.proc = zipfile.ZipFile(file) elif mode == 'rar': try: from unrar.rarfile import RarFile except ModuleNotFoundError: dprint( ) # 缺少unrar模块,安装详见: https://blog.csdn.net/code4101/article/details/79328636 raise ModuleNotFoundError self.proc = RarFile(file) # 3、解压文件夹目录,None表示还未解压 self.tempfolder = None
def rar_crack(self, pwd): runtime = self.show() print(f'破解已用时: {runtime} 当前密码: {pwd}', end='\r') try: rar_file = RarFile(self.filename, pwd=pwd) rar_file.extractall() print(f'破解已完成: {runtime} 压缩密码: {pwd}') with open('res.txt', 'w') as f: f.write(pwd) os._exit(0) except: pass
def run(self): for root, dirs, files in os.walk(self.comic_dir, topdown=True): for file in files: if file.endswith('.cbr'): file_name = file.split('.cbr')[0] zip_file_name = file_name + '.cbz' print(zip_file_name) comic = RarFile(file) comic.testrar() print(zip_file_name) zip_comic = ZipFile(zip_file_name, 'w') self.convert_rar_to_zip(comic, zip_comic) if file.endswith('.cbz'): zip_comic = ZipFile(file) self.gather_comic_info(zip_comic)
def _open_rarfile(self): rar_filename = os.path.join(TESTS_DIR, 'test_rar.rar') rar = RarFile(rar_filename) return rar
def _open_rarfile(self): rar_filename = os.path.join(TESTS_DIR, 'test_corrupted.rar') rar = RarFile(rar_filename) rar.setpassword('testing') return rar
def _open_rarfile(self): rar_filename = os.path.join(TESTS_DIR, 'test_password.rar') rar = RarFile(rar_filename, pwd='password') return rar