Extract Zipfile Using Python, Display Progress Percentage?
Solution 1:
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]
Solution 2:
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 = 0while 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
Solution 3:
Sorry a bit late seeing this. Had a similar problem, needing an equivalent to zipfile.Zipfile.extractall
. If you have tqdm>=4.40.0
(which I released over a year ago), then:
from os import fspath
from pathlib import Path
from shutil import copyfileobj
from zipfile import ZipFile
from tqdm.auto import tqdm # could use from tqdm.gui import tqdmfrom tqdm.utils import CallbackIOWrapper
defextractall(fzip, dest, desc="Extracting"):
"""zipfile.Zipfile(fzip).extractall(dest) with progress"""
dest = Path(dest).expanduser()
with ZipFile(fzip) as zipf, tqdm(
desc=desc, unit="B", unit_scale=True, unit_divisor=1024,
total=sum(getattr(i, "file_size", 0) for i in zipf.infolist()),
) as pbar:
for i in zipf.infolist():
ifnotgetattr(i, "file_size", 0): # directory
zipf.extract(i, fspath(dest))
else:
with zipf.open(i) as fi, open(fspath(dest / i.filename), "wb") as fo:
copyfileobj(CallbackIOWrapper(pbar.update, fi), fo)
Post a Comment for "Extract Zipfile Using Python, Display Progress Percentage?"