I was wondering, how can I get the percentage of this being done, so I can display it on a progress bar?
ZipFile.CreateFromDirectory("C:\temp\folder", "C:\temp\folder.zip")
and also
ZipFile.ExtractToDirectory("C:\temp\folder.zip", "C:\temp\folder")
This doesnt have any events or callbacks that you can use to report progress. Simply means you cant with the .Net version. If you used the 7-Zip library you can do this easily.
I came across this question while checking for related questions for the identical question, asked for C# code. It is true that the .NET static
ZipFile
class does not offer progress reporting. However, it is not hard to do using theZipArchive
implementation, available since earlier versions of .NET.The key is to use a
Stream
wrapper that will report bytes read and written, and insert that in the data pipeline while creating or extracting the archive.I wrote a version in C# for an answer to the other question, and since I didn't find any VB.NET examples, figured it would be helpful to include a VB.NET version on this question.
(Arguably, I could include both examples in a single answer and propose closing one of the questions as a duplicate of the other. But since it's doubtful the close vote would result in an actual closure, the connection between the two questions would not be as obvious as it should be. I think for best visibility to future users trying to find the solution appropriate for their needs, leaving this as two different questions is better.)
The foundation of the solution is the
Stream
wrapper class:StreamWithProgress.vb
The wrapper class can be used to implement progress-aware versions of the
ZipFile
static methods:ZipFileWithProgress.vb
The .NET built-in implementation of
IProgress(Of T)
is intended for use in contexts where there is a UI thread where progress reporting events should be raised. As such, when used in a console program, like which I used to test this code, it will default to using the thread pool to raise the events, allowing for the possibility of out-of-order reports. To address this, the above uses a simpler implementation ofIProgress(Of T)
, one that simply invokes the handler directly and synchronously.BasicProgress.vb
And naturally, it's useful to have an example with which to test and demonstrate the code.
Module1.vb
Additional notes regarding this implementation can be found in my answer to the related question.