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