监控 ZIP 文件提取 Python

发布于 2024-09-29 12:53:36 字数 153 浏览 4 评论 0原文

我需要解压缩 .ZIP 存档。我已经知道如何解压缩它,但它是一个巨大的文件,需要一些时间才能解压。如何打印提取完成的百分比?我想要这样的东西:

Extracting File
1% Complete
2% Complete
etc, etc

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨落星ぅ辰 2024-10-06 12:53:37

这是一个您可以开始的示例,它没有经过优化:

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)

为了使其更美观,在打印时执行以下操作:

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

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),
梦巷 2024-10-06 12:53:37

您可以使用 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)

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)
御弟哥哥 2024-10-06 12:53:37

在 python 2.6 ZipFile 对象中有一个 open 方法,可以打开一个zip 中的命名文件作为文件对象,您可以起诉它以分块读取数据

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)

编辑:要获取总大小,您可以执行类似的操作

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

现在您可以打印总进度和每个文件的进度,例如假设您只有 1 zip 中的大文件,其他方法(例如仅计算文件大小并提取)根本不会提供任何进展。

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.

淡写薰衣草的香 2024-10-06 12:53:37

ZipFile.getinfolist() 将从 zip 文件的内容生成许多 ZipInfo 对象。从那里,您可以合计存档中所有文件的字节数,然后计算到目前为止已提取的文件数,也可以按文件总数进行计算。

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.

梦冥 2024-10-06 12:53:37

我不相信您可以跟踪提取单个文件的进度。 zipfile 提取函数 没有进度回调。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文