Cheeso, a save progress problem

Nov 30, 2009 at 9:08 AM

i compress all the files of a directory as following:

files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);   //Get all the files in directory
                emptyDirs = GetAllEmptyDirectories(dir);                // Get all the empty directory

                using (ZipFile zip = new ZipFile())
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        zip.Password = pwd;
                        zip.UseUnicodeAsNecessary = true;
                        zip.BufferSize = 4096;
                        zip.SaveProgress += this.SaveProgress;

                       
                        zip.AddFile(files[i]);
                    }

                    if (emptyDirs != null && emptyDirs.Length > 0)
                    {
                        for (int i = 0; i < emptyDirs.Length; i++)
                        {
                            zip.AddDirectoryByName(emptyDirs[i]);
                        }
                    }
                    zip.Save(savename);
                }

 

I want to make the progress value of the progressbar be 100% when all the files is compressed. for example, there are two files (test1.doc, and test2.doc), their size are same.  when test1.doc is compressed, the progress value should be 50%, when two are finished, the progress value should be 100%.

my SaveProgress event is as following:

private void SaveProgress(object sender, Ionic.Zip.SaveProgressEventArgs e)
        {
            if (stopFlg)
            {
                e.Cancel = true;
                return;
            }

            //long curSize = 0;

            switch(e.EventType)
            {

            //code

            }

}

when the length of files (file count)is 2, the ZipProgressEventType.Saving_AfterWriteEntry event of every file will be raised twice. when the length of files (file count) is n, the ZipProgressEventType.Saving_AfterWriteEntry event of every file will be raised n times. so when i compress 2000 files with the progress bar, the speed is very very slow, but when i compress 2000 files without progress bar, it finished fastly.

 

what can i do?? 

 

Coordinator
Dec 2, 2009 at 6:58 AM

I don't know why your save progress event is slow.  Usually it is not so signficant in performance. 

If you are switching to the UI thread for each progressbar update, that may incur some significant performance cost. 

You may wish to establish a scale of 100 for the progress bar.  When there are 2000 files, you can update the progress bar after every 20 files.

This may help.

If that does not help you, I suggest that you try the performance profiler, included in Visual Studio, to understand why the delay occurs for you.

In my tests I save zip files with 100,000 files or more, and the cost for save updates is not severe.

Dec 2, 2009 at 8:25 AM

thank you, cheeso, my usage is incorrect, "zip.SaveProgress += this.SaveProgress;" should be placed out of the for loop.

Coordinator
Dec 2, 2009 at 2:26 PM

ah, yes, that would do it.