[
  {
    "Id": "272877",
    "ThreadId": "79683",
    "Html": "<p>How do I update a ProgressBar to show the progress of a save operation?&nbsp; I am using this code to zip a folder:</p>\r\n<p><span style=\"font-size:x-small;color:#0000ff\">&nbsp;</span></p>\r\n<p><span style=\"font-size:x-small;color:#0000ff\"><span style=\"font-size:x-small;color:#0000ff\">Using</span></span><span style=\"font-size:x-small\"> zip </span><span style=\"font-size:x-small;color:#0000ff\"><span style=\"font-size:x-small;color:#0000ff\">As</span></span><span style=\"font-size:x-small\"> ZipFile = </span><span style=\"font-size:x-small;color:#0000ff\"><span style=\"font-size:x-small;color:#0000ff\">New</span></span><span style=\"font-size:x-small\"> ZipFile()</span><span style=\"font-size:x-small\">\r\n<p>\r\n<p>zip.AddDirectory(</p>\r\n</p>\r\n</span><span style=\"font-size:x-small;color:#a31515\"><span style=\"font-size:x-small;color:#a31515\">&quot;c:\\myFolder&quot;</span></span><span style=\"font-size:x-small\">)</span></p>\r\n<p>\r\n<p><span style=\"font-size:x-small\">zip.Save(</span><span style=\"font-size:x-small;color:#a31515\"><span style=\"font-size:x-small;color:#a31515\">&quot;c:\\temp\\MyZipFile.zip&quot;</span></span><span style=\"font-size:x-small\">)</span></p>\r\n<p><span style=\"font-size:x-small;color:#0000ff\"><span style=\"font-size:x-small;color:#0000ff\">End</span></span><span style=\"font-size:x-small\"> </span><span style=\"font-size:x-small;color:#0000ff\"><span style=\"font-size:x-small;color:#0000ff\">Using</span></span></p>\r\n<p>&nbsp;</p>\r\n<p><span style=\"color:#000000\">Do I need to use a BackgroundWorker?&nbsp; I looked at the code on the examples page but I couldn't figure out how to apply it for my case.&nbsp; Thanks.</span></p>\r\n</p>",
    "PostedDate": "2010-01-04T06:12:05.807-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "273024",
    "ThreadId": "79683",
    "Html": "<p>There are two things that take some time in the example code you offered. The AddDirectory() call can take some time if you have a directory of 10,000 files or more, then the Add operation can take some time.&nbsp; It involves memory operations, so it's won't take a huge amount of time, but 10,000 of anything will take some time.&nbsp; The second is the Save() operation - this involves writing to the filesystem, and will take more time.</p>\r\n<p>For the first, you can use an <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/842e7449-867a-2bed-dac5-4b7ac456d2ed.htm\">AddProgress</a> event, for the second, you can use a <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/2b8624b0-6823-7e33-a864-2c7bbd9835e7.htm\">SaveProgress</a> event.&nbsp; The events are just ways for DotNetZip to call your code - code you provide, as it makes progress through those operations.&nbsp; A callback.&nbsp; As the Save proceeds, it can call your code for every file added to the zip, for example.&nbsp; If you hook into the SaveProgress event, you can update a ProgressBar from within your code.&nbsp; Because the Add operation generally happens so much faster than the Save(), you likely won't need a progress event for the Add.&nbsp;</p>\r\n<p>Now, the question of whether you need a BackgroundWorker is somewhat independent.&nbsp; It depends.&nbsp;</p>\r\n<p>If you have a directory of,say 1000 files totally maybe 20mb, then no, you probably won't need&nbsp; a background worker. The entire Add() and Save() operation will take maybe 5 or 6 seconds, and your UI could just put an hourglass up for that time, and the user won't be terribly inconveniced.&nbsp;</p>\r\n<p>On the other hand if the directory is 10,000 files or more and can constitute a file of 1gb or more, then you will need a BackgroundWorker.&nbsp; But not simply for the progress bar - you need a BackgroundWorker to preserve UI responsiveness while the operation occurs, and following good UI principles, you will need to provide a Cancel button to interrupt the long-running process if the user chooses to do so.&nbsp; In the general case, you want to do the zipping on a BackgroundWorker.&nbsp;&nbsp;</p>\r\n<p>Anyway, there's a nice VB example that covers this - it shows how to use a SaveProgress event and a BackgroundWorker.&nbsp; It's a VB project for VS2008.&nbsp;</p>\r\n<p>To get this you need to download the latest source snapshot at&nbsp; <a href=\"http://dotnetzip.codeplex.com/SourceControl/changeset/view/53555\">http://dotnetzip.codeplex.com/SourceControl/changeset/view/53555</a>&nbsp;.&nbsp;&nbsp; I had thought it was part of the regular &quot;released&quot; source zip, but it is not.&nbsp; Not yet.&nbsp; so for now, you need to go to that snapshot. Click the &quot;Source code&quot; tab on the dotnetzip project site, and then navigate to the example, as shown in the image here:</p>\r\n<p><img src=\"http://i48.tinypic.com/20k5c1g.jpg\" alt=\"\"></p>\r\n<p>&nbsp;The most interesting bits of code are here:</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Sub</span> Button2_Click(<span style=\"color:Blue\">ByVal</span> sender <span style=\"color:Blue\">As</span> System.Object, <span style=\"color:Blue\">ByVal</span> e <span style=\"color:Blue\">As</span> System.EventArgs) <span style=\"color:Blue\">Handles</span> btnZipUp.Click\r\n        <span style=\"color:Blue\">Me</span>.KickoffZipup()\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n\r\n\r\n    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Sub</span> KickoffZipup()\r\n        <span style=\"color:Blue\">Dim</span> folderName <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span> = <span style=\"color:Blue\">Me</span>.tbDirToZip.Text\r\n        <span style=\"color:Blue\">If</span> (((<span style=\"color:Blue\">Not</span> folderName <span style=\"color:Blue\">Is</span> <span style=\"color:Blue\">Nothing</span>) <span style=\"color:Blue\">AndAlso</span> (folderName &lt;&gt; <span style=\"color:#A31515\">&quot;&quot;</span>)) <span style=\"color:Blue\">AndAlso</span> ((<span style=\"color:Blue\">Not</span> <span style=\"color:Blue\">Me</span>.tbZipToCreate.Text <span style=\"color:Blue\">Is</span> <span style=\"color:Blue\">Nothing</span>) <span style=\"color:Blue\">AndAlso</span> (<span style=\"color:Blue\">Me</span>.tbZipToCreate.Text &lt;&gt; <span style=\"color:#A31515\">&quot;&quot;</span>))) <span style=\"color:Blue\">Then</span>\r\n            <span style=\"color:Blue\">If</span> File.Exists(<span style=\"color:Blue\">Me</span>.tbZipToCreate.Text) <span style=\"color:Blue\">Then</span>\r\n                <span style=\"color:Blue\">If</span> (MessageBox.Show(<span style=\"color:Blue\">String</span>.Format(<span style=\"color:#A31515\">&quot;The file you have specified ({0}) already exists.  Do you want to overwrite this file?&quot;</span>, _\r\n                                                  <span style=\"color:Blue\">Me</span>.tbZipToCreate.Text), <span style=\"color:#A31515\">&quot;Confirmation is Required&quot;</span>, _\r\n                                                  MessageBoxButtons.YesNo, MessageBoxIcon.Question) &lt;&gt; DialogResult.Yes) <span style=\"color:Blue\">Then</span>\r\n                    <span style=\"color:Blue\">Return</span>\r\n                <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n                File.Delete(<span style=\"color:Blue\">Me</span>.tbZipToCreate.Text)\r\n            <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n            <span style=\"color:Blue\">Me</span>._saveCanceled = <span style=\"color:Blue\">False</span>\r\n            <span style=\"color:Blue\">Me</span>._nFilesCompleted = 0\r\n            <span style=\"color:Blue\">Me</span>._totalBytesAfterCompress = 0\r\n            <span style=\"color:Blue\">Me</span>._totalBytesBeforeCompress = 0\r\n            <span style=\"color:Blue\">Me</span>.btnZipUp.Enabled = <span style=\"color:Blue\">False</span>\r\n            <span style=\"color:Blue\">Me</span>.btnZipUp.Text = <span style=\"color:#A31515\">&quot;Zipping...&quot;</span>\r\n            <span style=\"color:Blue\">Me</span>.btnCancel.Enabled = <span style=\"color:Blue\">True</span>\r\n            <span style=\"color:Blue\">Me</span>.lblStatus.Text = <span style=\"color:#A31515\">&quot;Zipping...&quot;</span>\r\n            <span style=\"color:Blue\">Dim</span> options <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">New</span> WorkerOptions\r\n            options.ZipName = <span style=\"color:Blue\">Me</span>.tbZipToCreate.Text\r\n            options.Folder = folderName\r\n            \r\n            _backgroundWorker1 = <span style=\"color:Blue\">New</span> System.ComponentModel.BackgroundWorker()\r\n            _backgroundWorker1.WorkerSupportsCancellation = <span style=\"color:Blue\">False</span>\r\n            _backgroundWorker1.WorkerReportsProgress = <span style=\"color:Blue\">False</span>\r\n            <span style=\"color:Blue\">AddHandler</span> <span style=\"color:Blue\">Me</span>._backgroundWorker1.DoWork, <span style=\"color:Blue\">New</span> DoWorkEventHandler(<span style=\"color:Blue\">AddressOf</span> <span style=\"color:Blue\">Me</span>.DoSave)\r\n            _backgroundWorker1.RunWorkerAsync(options)\r\n            \r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n\r\n    \r\n    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Sub</span> DoSave(<span style=\"color:Blue\">ByVal</span> sender <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">Object</span>, <span style=\"color:Blue\">ByVal</span> e <span style=\"color:Blue\">As</span> DoWorkEventArgs)\r\n        <span style=\"color:Blue\">Dim</span> options <span style=\"color:Blue\">As</span> WorkerOptions = e.Argument\r\n        <span style=\"color:Blue\">Try</span>\r\n            <span style=\"color:Blue\">Using</span> zip1 <span style=\"color:Blue\">As</span> ZipFile = <span style=\"color:Blue\">New</span> ZipFile\r\n                zip1.AddDirectory(options.Folder)\r\n                <span style=\"color:Blue\">Me</span>._entriesToZip = zip1.EntryFileNames.<span style=\"color:Blue\">Count</span>\r\n                <span style=\"color:Blue\">Me</span>.SetProgressBars()\r\n                <span style=\"color:Blue\">AddHandler</span> zip1.SaveProgress, <span style=\"color:Blue\">New</span> EventHandler(Of SaveProgressEventArgs)(<span style=\"color:Blue\">AddressOf</span> <span style=\"color:Blue\">Me</span>.zip1_SaveProgress)\r\n                zip1.Save(options.ZipName)\r\n            <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Using</span>\r\n        <span style=\"color:Blue\">Catch</span> exc1 <span style=\"color:Blue\">As</span> Exception\r\n            MessageBox.Show(<span style=\"color:Blue\">String</span>.Format(<span style=\"color:#A31515\">&quot;Exception while zipping: {0}&quot;</span>, exc1.Message))\r\n            <span style=\"color:Blue\">Me</span>.btnCancel_Click(<span style=\"color:Blue\">Nothing</span>, <span style=\"color:Blue\">Nothing</span>)\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Try</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n\r\n</pre>\r\n</div>",
    "PostedDate": "2010-01-04T11:55:24.973-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "273037",
    "ThreadId": "79683",
    "Html": "<p>So how do I set the Maximum value for my progressbar to track the AddDirectory progress?&nbsp; It appears that I can't get the number of files</p>\r\n<p>in the zip object until AFTER the AddDirectory process has completed.</p>",
    "PostedDate": "2010-01-04T12:27:10.58-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "273049",
    "ThreadId": "79683",
    "Html": "<p>Well that's correct.&nbsp; There's no way for the library to know how many files it will find in the directory until it finds them in the directory, if you know what I mean.</p>\r\n<p>And as soon as DotNetZip finds a file in the directory, it adds the file to the archive and then invokes the AddProgress event.&nbsp; so .. To drive a progress bar based on AddProgress, you would need an indefinite progress indicator.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-01-04T12:53:03.617-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "273051",
    "ThreadId": "79683",
    "Html": "<p>OK, so just to clarify ... there is no way to track the progress (percentage wise) of the AddDirectory process?&nbsp; Only the Save process can be tracked?</p>\r\n<p>Is there any practical purpose for the AddProgress event?</p>",
    "PostedDate": "2010-01-04T13:02:26-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "273059",
    "ThreadId": "79683",
    "Html": "<p>Well sure, there's a practical purpose. I just told you.&nbsp; It gives you a call for every entry.&nbsp; That is its practical purpose. I take it you don't value that purpose, but that is a purpose, whether you like it or not.</p>\r\n<p>I think you mean;&nbsp; &quot;is there a way to know how many entries will eventually be added as AddDirectory proceeds, so that I can drive a traditional progressbar?&quot;</p>\r\n<p>You can do this, if you know how many total entries will be added in the beginning.&nbsp; One way to do this is to call Directory.GetFiles, and remember the number of files in the directory before calling AddDirectory.&nbsp;</p>\r\n<p>Or, pass a finite IEnumerable&lt;String&gt; to the AddFiles() method, and in your code you know how many items are in the IEnumerable.&nbsp;</p>\r\n<p>Then you can set ProgressBar.MaximumValue.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-01-04T13:26:16.037-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]