By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

Zip file writing progress (callback proc)

P: n/a
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
Mar 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mar 26, 4:41 pm, durumdara <durumd...@gmail.comwrote:
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
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
Mar 26 '07 #2

P: n/a
durumdara wrote:
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
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
Mar 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.