I know how to extract a zip archive using Python, but how exactly do I display the progress of that extraction in a percentage?
Advertisement
Answer
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)
this extracts entry_name
to target_name
most of this is also done by shutil.copyfileobj
but it doesn’t have a call back for progress either
the source of the ZipFile.extract
method calls _extract_member
uses:
source = self.open(member, pwd=pwd) target = file(targetpath, "wb") shutil.copyfileobj(source, target) source.close() target.close()
where member has be converted from a name to a ZipInfo object by getinfo(member)
if it wasn’t a ZipInfo object