I've been trying to work with Ionic.Zip (DotNetZip) for extracting downloaded zip files through a program. But I'm stuck when trying to detect when the Extraction process as completed. The library contains a ExtractProgress event, so I tried to detect it via a progress bar, but it updates the progress bar based on every entry as a single entry and not the zip as a whole. So as soon as the first file entry is unzipped it would consider it "Completed". Does anyone know a way to detect Extraction of a entire zip completion with DotNetZip?
4 Answers
From a quick scan of the examples on the DotNetZip Codeplex it looks you unpack the zip one item at a time. To report the progress to the user, use a BackgroundWorker
like so.
public void ExtractFile(string zipToUnpack, string unpackDirectory)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.ProgressChanged += (o, e) => { Console.WriteLine("Percent completed:" + e.ProgressPercentage); };
worker.DoWork += (o, e) =>
{
using (ZipFile zip = ZipFile.Read(zipToUnpack))
{
int step = (zip.Count / 100.0);
int percentComplete = 0;
foreach (ZipEntry file in zip)
{
file.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
percentComplete += step;
worker.ReportProgress(percentComplete);
}
}
};
worker.RunWorkerAsync();
}
Also, just looked at the source code on Codeplex and it is a rather complete product so there should be many ways to achieve what you want; what I have given is only one of them.
HTH,

- 20,275
- 4
- 64
- 80
-
@Cheeso: I just fixed a compile error in the example: `worker.ProgressChanged += (o, e) { Console` should be `worker.ProgressChanged += (o, e) => { Console`. – Dennis Aug 09 '11 at 18:05
If you have code like this:
using (var zip = ZipFile.Read("whatever.zip"))
{
zip.ExtractProgress += MyExtractProgress;
numEntries = zip.Entries.Count;
foreach (var e in zip)
{
e.Extract();
}
}
...you will get extract progress events. The ExtractProgressEventArgs class is documented here. In the event args class, there is an EventType field which describes which event you've received. These types are described here.
There are event types for before extraction begins for an entry, after extraction completes (for an entry). There is also an event type that fires for each chunk of data that gets decompressed, if you care to track to that level. Because of that, You can get thousands of events for a particular extraction. If you track the number of entries, then you know how many "after" events to expect, and you can declare extraction completed when you have received the appropriate number of after events.
The library cannot issue a "extract is completed" event if you are extracting each entry in a loop, as I've shown above. This is because it does not know if you will continue your loop to the end, or maybe stop in the middle. It cannot know.
If you call zip.ExtractAll(), like this:
using (var zip = ZipFile.Read("whatever.zip"))
{
zip.ExtractProgress += MyExtractProgress;
zip.ExtractAll(extractDirectory);
}
...then DotNetZip will issue a "all entries have been extracted" event type. This is also described in the documentation links I provided above.
So you have some options.

- 189,189
- 101
- 473
- 713
-
what exactly is MyExtractProgress, and btw it gives me error: Zipfile doesnt contain definition for "Read" – Nick Dec 11 '16 at 08:58
There is an EventType called Extracting_AfterExtractAll which you can use to check if the ExtractAll operation is completed:
private void UnZipFile(zipFilePath)
{
using (ZipFile zip = new ZipFile(zipFilePath))
{
zip.ExtractProgress += zip_ExtractProgress;
zip.ExtractAll(Path.GetDirectoryName(zipFilePath));
}
}
static void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Extracting_AfterExtractAll)
zipExtractFinished = true;
}

- 2,937
- 3
- 36
- 61
Instead of using your own worker for this.. there's also the ExtractProgress event the submitter referred to, plus it's possible to query the number of items in the zip. Combine those two and you've got your progress event and the ability to track progress against the total number of items. AFAIK there is no event that indicates end-of-extraction.

- 3,610
- 2
- 33
- 55