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=Fals e, callback=None):
"""Open the ZIP file with mode read "r", write "w" or append "a"."""
self._allowZip6 4 = 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(b uf)
compress_size = compress_size + len(buf)
self.fp.write(b uf)
3) Define a callback class and use it:
import zipfile
class CB():
def progress(self, total, sofar):
zippercentcompl ete=100.0*sofar/total
print "callback.progr ess.zippercentc omplete=%.2f" % zippercentcompl ete
return
z=zipfile.ZipFi le(r'C:\testzip .zip', mode='w', callback=CB())
z.write(r'c:\Li brary\TurboDelp hi\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