smihica / pyminizip

To create a password encrypted zip file in python.
Other
106 stars 37 forks source link

Add optional progress callback to compress_multiple() #9

Closed andrewleech closed 8 years ago

andrewleech commented 8 years ago

This patch allows the progress of a compress_multiple() to be monitored. The optional new argument to compress_multiple is a function that accepts one argument, the count of files currently processed.

Usage is as such:

files = [u'pyminizip.so', 'file2.txt']
def progress(count):
    print "Compressed %d of %d" % (count, len(files))

pyminizip.compress_multiple(files, "file.zip", "1233", 4, progress)
smihica commented 8 years ago

Thank you for your nice work!! @andrewleech ;) What are you thinking of the use case of this changes? logging ??

I don't think of anything now... sorry.

andrewleech commented 8 years ago

I'm using pyminizip in a thread in a python gui application with a progress() function which updates a progress bar on the gui while it's compressing a large folder of text files.

But similarly, in a command line python script the progress function could be used like:

from __future__ import print_function
import os
import pyminizip

files = [f for f in os.listdir('.') if os.path.isfile(f)]

def progress(count):
    percent = 100.0 * count / len(files)
    prog = '#' * int(percent)
    prog += ' ' * (100 - len(prog))
    print ('\r[%s] %0.2f%%' % (prog, percent), end="")

pyminizip.compress_multiple(files, "archive.zip", "1233", 4, progress)

This displays a nice pretty progress bar on the command line while it's compressing all the files in the current folder.

smihica commented 8 years ago

Oh I see. It may be able to use on webapplication's progress bar too. Thank you for your nice commit!