import zipfile def read_zip(zip_file_path: str, unpack_path: str, ws_msg: WebSocketMsg): """ 解壓ZIP文件 @param zip_file_path: ZIP文件路徑(ex. E:\aaa\a.zip) @param unpack_path: 解壓文件輸出路徑(ex. E:\aaa) @param ws_msg: 用來放實時進度的類(可干掉) """ file_list = zipfile.ZipFile(zip_file_path) info = file_list.infolist() ''' 1 計算解壓后的文件總大小(單位:字節B) ''' all_size = 0 for i in info: all_size += i.file_size # 1.1 字節B轉換為兆字節MB (字符串) all_size_str = str(int(all_size / 1024 / 1024)) + 'MB' ''' 2 當前已解壓的文件總大小(單位:字節B) ''' now_size = 0 for i in info: file_list.extract(i, unpack_path) now_size += i.file_size # 2.1 字節B轉換為兆字節MB (字符串) now_size_str = str(int(now_size / 1024 / 1024)) + 'MB' ws_msg.msg.append(f'解壓進度:{int(now_size / all_size * 100)}% ({now_size_str}/{all_size_str})') # print(f'解壓進度:{int(now_size / all_size * 100)}% ({now_size_str}/{all_size_str})') file_list.close()