Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract ZipFile using Python, display Progress Percentage?

I know how to extract a zip archive using Python, but how exactly do I display the progress of that extraction in a percentage?

like image 741
Zac Brown Avatar asked Dec 03 '10 01:12

Zac Brown


People also ask

How do I extract a ZipFile in Python?

extractall() method will extract all the contents of the zip file to the current working directory. You can also call extract() method to extract any file by specifying its path in the zip file. This will extract only the specified file.

What is ZipFile in Python?

Python's zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.

How do I zip multiple files in Python?

Create a zip archive from multiple files in PythonCreate a ZipFile object by passing the new file name and mode as 'w' (write mode). It will create a new zip file and open it within ZipFile object. Call write() function on ZipFile object to add the files in it. call close() on ZipFile object to Close the zip file.


2 Answers

I suggest using tqdm, you can install it using pip like so:

pip install tqdm

Then, you can use it directly like so:

>>> from tqdm import tqdm
>>>
>>> with zipfile.ZipFile(some_source) as zf:
...     for member in tqdm(zf.infolist(), desc='Extracting '):
...         try:
...             zf.extract(member, target_path)
...         except zipfile.error as e:
...             pass

This will produce something like so:

Extracting : 100%|██████████| 60.0k/60.0k [14:56<00:00, 66.9File/s]
like image 87
Anwarvic Avatar answered Oct 19 '22 19:10

Anwarvic


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

like image 8
Dan D. Avatar answered Oct 19 '22 19:10

Dan D.