[ACCEPTED]-Monitor ZIP File Extraction Python-extraction
here an example that you can start with, it's 2 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 1 do this when printing:
print "%s %%\r" % (extracted_size * 100/uncompress_size),
You can just monitor the progress of each 1 file being extracted with tqdm()
:
from zipfile import ZipFile
from tqdm import tqdm
# Open your .zip file
with ZipFile(file=path) as zip_file:
# Loop over each file
for file in tqdm(iterable=zip_file.namelist(), total=len(zip_file.namelist())):
# Extract each file to another directory
# If you want to extract to current working directory, don't specify path
zip_file.extract(member=file, path=directory)
In python 2.6 ZipFile object has a open method 9 which can open a named file in zip as a 8 file object, you can sue that to read data 7 in chunks
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 6 can do something like this
total_size = sum((file.file_size for file in zf.infolist()))
So now you can 5 print the total progress and progress per 4 file, e.g. suppose you have only 1 big file 3 in zip, other methods(e.g. just counting 2 file sizes and extract) will not give any 1 progress at all.
ZipFile.getinfolist()
will generate a number of ZipInfo
objects from 6 the contents of the zip file. From there 5 you can either total up the number of bytes 4 of all the files in the archive and then 3 count up how many you've extracted thus 2 far, or you can go by the number of files 1 total.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.