Monitoring Python ZIP file extracts

I need to unzip the .ZIP file. I already know how to unzip it, but it is a huge file and takes some time to extract it. How to print a percentage extraction kit? I would like something like this:

Extracting File 1% Complete 2% Complete etc, etc 
+6
python extraction zip monitor progress
source share
4 answers

here is an example from which you can start, it is not optimized:

 import zipfile zf = zipfile.ZipFile('test.zip') uncompress_size = sum((file.file_size for file in zf.infolist())) extracted_size = 0 for file in zf.infolist(): extracted_size += file.file_size print "%s %%" % (extracted_size * 100/uncompress_size) zf.extract(file) 

to make it more beautiful, do it when printing:

  print "%s %%\r" % (extracted_size * 100/uncompress_size), 
+11
source share

In python 2.6, a ZipFile object has an open method that can open a named file in zip as a file object, you can sue it to read the data in pieces

 import zipfile import os def read_in_chunks(zf, name): chunk_size= 4096 f = zf.open(name) data_list = [] total_read = 0 while 1: data = f.read(chunk_size) total_read += len(data) print "read",total_read if not data: break data_list.append(data) return "".join(data_list) zip_file_path = r"C:\Users\anurag\Projects\untitled-3.zip" zf = zipfile.ZipFile(zip_file_path, "r") for name in zf.namelist(): data = read_in_chunks(zf, name) 

Edit: To get the total size, you can do something like this

 total_size = sum((file.file_size for file in zf.infolist())) 

So, now you can print the overall progress and progress for each file, for example. Suppose you only have 1 large file in zip, other methods (e.g. just counting file sizes and extracting) will not make any progress.

+2
source share

ZipFile.getinfolist() will generate several ZipInfo objects from the contents of the zip file. From there, you can either sum the number of bytes of all the files in the archive, and then calculate how much you have extracted at the moment, or you can specify the number of files.

0
source share

I do not believe that you can track the progress of extracting a single file. The zipfile extract function does not have a callback for progress.

-one
source share

All Articles