import os from log import logger, log_info # 需要安装winrar和7z并且配置环境变量 class UnzipFile: def __init__(self): pass # 处理zip和rar @staticmethod def unzip_zip(source, password=""): # 准备参数 cmd = 'WinRAR.exe e -y -ibck -ilog.\\winrar.log' files = '*.*' args = '-or' if password: pwd = '-hp"' + str(password).strip() + '"' else: pwd = '' target = os.path.splitext(source)[0] + '\\' # 组装参数 cmd_list = [cmd, '"' + source + '"', files, '"' + target + '"', pwd, args] cmd_str = ' '.join(cmd_list) # 打印日志 log_info(cmd_str) # 解压并处理结果 r = os.system(cmd_str) if r == 0 and os.path.exists(target): return True return False # 处理7z相关 @staticmethod def unzip_7z(source, password=""): # 准备参数 cmd = '7z.exe x -y ' args = '-o' if password: pwd = '-p"' + str(password) + '"' else: pwd = '' target = os.path.splitext(source)[0] + '\\' # 组装参数 cmd_list = [cmd, '"' + source + '"', args + '"' + target + '"', pwd] cmd_str = ' '.join(cmd_list) # 打印日志 log_info(cmd_str) # 解压并处理结果 r = os.system(cmd_str) if r == 0 and os.path.exists(target): return True return False # 打包一个通用方法 def unzip(self, source, password=""): ext = os.path.splitext(source)[1] if ext == '.7z' or ext == '.001' or ext == '.wim': result = self.unzip_7z(source, password) else: result = self.unzip_zip(source, password) log_info(f'解压结果: {result}') return result if __name__ == '__main__': pass