您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python提取ZipFile,显示进度百分比?

使用Python提取ZipFile,显示进度百分比?

extract方法没有为此提供回调,因此必须使用它getinfo获取e的未压缩大小,然后以块为单位打开从文件中读取的文件,然后将其写入到您要删除文件中并更新百分比如果需要的话,还必须还原mtime:

import zipfile
z = zipfile.ZipFile(some_source)
entry_info = z.getinfo(entry_name)
i = z.open(entry_name)
o = open(target_name, 'w')
offset = 0
while True:
    b = i.read(block_size)
    offset += len(b)
    set_percentage(float(offset)/float(entry_info.file_size) * 100.)
    if b == '':
        break
    o.write(b)
i.close()
o.close()
set_attributes_from(entry_info)

提取entry_nametarget_name

大部分操作也是通过此操作完成的,shutil.copyfileobj但也没有回呼进度

ZipFile.extract方法调用的源_extract_member使用:

source = self.open(member, pwd=pwd)
target = file(targetpath, "wb")
shutil.copyfileobj(source, target)
source.close()
target.close()

如果成员不是ZipInfo对象,getinfo(member)则该成员已从名称转换为ZipInfo对象

python 2022/1/1 18:47:30 有351人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶