Monitor ZIP File Extraction Python

╄→гoц情女王★ 提交于 2019-12-12 07:58:22

问题


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

Extracting File
1% Complete
2% Complete
etc, etc

回答1:


here an example that you can start with, it's 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 this when printing:

 print "%s %%\r" % (extracted_size * 100/uncompress_size),



回答2:


In python 2.6 ZipFile object has a open method which can open a named file in zip as a file object, you can sue that to read data 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 can do something like this

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

So now you can print the total progress and progress per file, e.g. suppose you have only 1 big file in zip, other methods(e.g. just counting file sizes and extract) will not give any progress at all.




回答3:


You can just monitor the progress of each 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)



回答4:


ZipFile.getinfolist() will generate a number of ZipInfo objects from the contents of the zip file. From there you can either total up the number of bytes of all the files in the archive and then count up how many you've extracted thus far, or you can go by the number of files total.




回答5:


I don't believe you can track the progress of extracting a single file. The zipfile extract function has no callback for progress.



来源:https://stackoverflow.com/questions/4006970/monitor-zip-file-extraction-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!