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()

 

相关文章:

  • 2022-12-23
  • 2021-08-07
  • 2021-11-16
  • 2021-12-04
  • 2021-09-27
  • 2022-01-13
  • 2021-06-20
猜你喜欢
  • 2021-11-21
  • 2021-11-30
  • 2021-08-10
  • 2021-11-25
  • 2021-12-13
  • 2021-10-11
相关资源
相似解决方案