使用Python提取ZipFile,显示进度百分比? [英] Extract ZipFile using Python, display Progress Percentage?

查看:725
本文介绍了使用Python提取ZipFile,显示进度百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用Python提取zip归档文件,但是我如何确切地以百分比显示提取进度?

I know how to extract a zip archive using Python, but how exactly do I display the progress of that extraction in a percentage?

推荐答案

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

the extract method doesn't provide a call back for this so one would have to use getinfo to get the e uncompressed size and then open the file read from it in blocks and write it to the place you want the file to go and update the percentage one would also have to restore the mtime if that is wanted an example:

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_name提取到target_name

大多数操作也由shutil.copyfileobj完成,但也没有回调进度

most of this is also done by shutil.copyfileobj but it doesn't have a call back for progress either

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对象

where member has be converted from a name to a ZipInfo object by getinfo(member) if it wasn't a ZipInfo object

这篇关于使用Python提取ZipFile,显示进度百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆