Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Zip file writing progress (callback proc)

213 views
Skip to first unread message

durumdara

unread,
Mar 26, 2007, 9:41:10 AM3/26/07
to pytho...@python.org
Hi!

I want to check my zip file writings.
I need some callback procedure to show a progress bar.
Can I do that?
I don't want to modify the PyLib module to extend it, because if I get
another py, the changes are lost.
This happening too if I copy the zip module to modify it.
Any solution?

Thanks for it:
dd

irs...@gmail.com

unread,
Mar 26, 2007, 11:03:49 AM3/26/07
to

Would it be enough to show progress based on how many files have
been added to the zip? If you're zipping just one huge file, I'm
afraid you can't get a progress bar using the zipfile module.

If it's not necessary that the one file is a zip but simply
compressed,
you can use zlib or bz2 modules to compress incrementally:
http://www.python.org/doc/lib/node303.html


Larry Bates

unread,
Mar 27, 2007, 12:35:24 PM3/27/07
to

I did this by extending the write method of my zipfile module.

1) Add a callback= keyword argument to __init__ and saving that in
an instance variable (self._callback).

def __init__(self, file, mode="r", compression=ZIP_STORED,
allowZip64=False, callback=None):

"""Open the ZIP file with mode read "r", write "w" or append "a"."""
self._allowZip64 = allowZip64
self._didModify = False
self._callback = callback # new instance variable to be used in write


2) Change write method so that it calls the progress method of the callback
function after every block is written (if user passed in one).

while 1:
buf = fp.read(1024 * 8)
if not buf:
break
file_size = file_size + len(buf)
#-----New lines follow
#
# Call the progress method of the callback function (if defined)
#
if self._callback is not None:
self._callback.progress(st.st_size, fp.tell())

#-----End of new lines
CRC = binascii.crc32(buf, CRC)
if cmpr:
buf = cmpr.compress(buf)
compress_size = compress_size + len(buf)
self.fp.write(buf)

3) Define a callback class and use it:

import zipfile

class CB():
def progress(self, total, sofar):
zippercentcomplete=100.0*sofar/total
print "callback.progress.zippercentcomplete=%.2f" % zippercentcomplete
return

z=zipfile.ZipFile(r'C:\testzip.zip', mode='w', callback=CB())
z.write(r'c:\Library\TurboDelphi\TurboDelphi.exe')

At least by doing it this way you won't break anything if you get a new zipfile
module. It just won't show progress any more and then you can patch it.

Hope info helps.

-Larry Bates

0 new messages