views:

278

answers:

2

Hi, this has to be really easy, and it certainly seems to be a very frequently asked question, but I can't for the life of me find a 'straightforward' answer.

I want to create a ProgressBar that shows a Zip file being extracted by SharpZipLib.

The FastZip and FastZipEvents classes give progress on individual files but not on position within the overall Zip. That is, if there Zip contains 200 files, what file is currently being extracted. I don't care about the progress through individual files (e.g. 20KB through 43KB in Foo.txt).

I think I could fudge a way of doing this by first creating a ZipFile and to access the Count property. And then... using ZipInputStream or FastZip to extract and keep progress count myself but I think that means the Zip is effectively unzipped twice (once entirely into memory) and I don't like that.

Any clean way of doing this?

+1  A: 

Regarding your last sentence: "I think that means the Zip is effectively unzipped twice".

Reading the content table of a zip file doesn't cost a lot at all (and doesn't access the contained files. You probably noticed that when you looked at a zip file with a "password" and only needed to enter the password when you tried to extract a file. You can look at the entries/content table just fine).

So I see nothing wrong with the approach of first checking the index/content table, storing the entry count (maybe even with compressed/uncompressed size?) and using the stream based api later.

Benjamin Podszun
A: 

FYI: DotNetZip has ExtractProgress event for this sort of thing. Code:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  zip.ExtractProgress = MyExtractProgress; 
  zip.ExtractAll(TargetDirectory);    
}

The extractprogress handler looks like this:

    private void MyExtractProgress(object sender, ExtractProgressEventArgs e)
    {
        switch (e.EventType)
        {
            case ZipProgressEventType.Extracting_BeforeExtractEntry:
            ....
            case ZipProgressEventType.Extracting_EntryBytesWritten:
            ...
            case ZipProgressEventType.Extracting_AfterExtractEntry:
            ....
        }
    }

You could use it to drive the familiar 2-progressbar UI, with one bar showing progress for the archive, and another bar showing progress for the individual file within the archive.

Cheeso