Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For some reason when I try to unpack a zip it does not update the progress bar any help would be welcomed.

VB
Imports Ionic.Zip
Imports System.Threading

Public Class Form1

    Private Property CurrentCount As Integer
    Private Property TotalCount As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Delegate Sub SetStatusTextInvoker(ByVal Text As String)
    Private Sub SetStatusText(ByVal Text As String)
        If Me.InvokeRequired Then
            Me.Invoke(New SetStatusTextInvoker(AddressOf SetStatusText), Text)
        Else
            Label1.Text = Text
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim TotalSize As Long
        Dim ZipToUnpack As String = "C:\temp\1.zip"
        Dim extractDir As String = "C:\temp\extract"

        Try
            Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
                AddHandler (zip.ExtractProgress), New EventHandler(Of ExtractProgressEventArgs)(AddressOf Zip_ExtractProgress)

                For Each Entry As ZipEntry In zip.Entries
                    TotalSize += Entry.UncompressedSize
                Next
                For Each Entry As ZipEntry In zip.Entries
                    Entry.Extract(extractDir, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
                Next
            End Using
        Catch EX As Exception
            MessageBox.Show(EX.Message)
        End Try
    End Sub

    Private Sub Zip_ExtractProgress(ByVal sender As Object, ByVal e As ExtractProgressEventArgs)
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True 'If we press the stop button, reset the variables we used for extracting, then invoke Cancel. RunWorkerCompleted will be called.
        End If

        Select Case e.EventType
            Case ZipProgressEventType.Extracting_EntryBytesWritten

                Dim Entry As Integer
                Dim CurrentCount As Integer = Entry
    
                BackgroundWorker1.ReportProgress(CInt(Int((100 * (CurrentCount / TotalCount)))))
        End Select
    End Sub
End Class
Posted
Updated 25-Jan-17 2:55am

Try this code and see if is helping you, i use only one ProgressBar.

Imports Ionic.Zip
Imports System.Threading
Imports System.IO


Public Class Form1

    Private CurrentCount As Integer = 0
    Private TotalCount As Long = 0, Total As Long = 0, LastVal As Long = 0, Sum As Long = 0


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Maximum = CInt(TotalCount)
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Delegate Sub SetStatusTextInvoker(ByVal Text As String)
    Private Sub SetStatusText(ByVal Text As String)
        System.Threading.Thread.Sleep(6)
        If Me.InvokeRequired Then
            Me.Invoke(New SetStatusTextInvoker(AddressOf SetStatusText), Text)
        Else
            Label1.Text = Text
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ExtractZip("C:\test\test.zip", "C:\test\extract\")
    End Sub

    Public Sub ExtractZip(ByVal szFileZip As String, ByVal ExtractTo As String)

        ' This will delete temp file when you cancel the extraction.
        Dim szGetFiles As Collections.ObjectModel.ReadOnlyCollection(Of String)
        Dim FilePath As String
        szGetFiles = My.Computer.FileSystem.GetFiles("C:\test\", FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
        For Each FilePath In szGetFiles
            File.Delete(FilePath)
        Next
        Try
            CurrentCount = 0
            Using MyZip As ZipFile = ZipFile.Read(szFileZip)
                AddHandler (MyZip.ExtractProgress), New EventHandler(Of ExtractProgressEventArgs)(AddressOf Zip_ExtractProgress)
                For Each Entry As ZipEntry In MyZip
                    CurrentCount += 1
                    TotalCount += Entry.UncompressedSize
                Next Entry
                MyZip.ExtractAll(ExtractTo, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
            End Using
        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Zip_ExtractProgress(ByVal sender As Object, ByVal e As ExtractProgressEventArgs)
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True 'If we press the stop button, reset the variables we used for extracting, then invoke Cancel. RunWorkerCompleted will be called.
        End If
        System.Windows.Forms.Application.DoEvents()
        If Total <> e.TotalBytesToTransfer Then
            SetStatusText(e.CurrentEntry.FileName)
            Sum += Total - LastVal + e.BytesTransferred
            Total = e.TotalBytesToTransfer
        Else
            Sum += e.BytesTransferred - LastVal
        End If
        LastVal = e.BytesTransferred
        BackgroundWorker1.ReportProgress(CInt((Sum)))
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        ProgressBar1.Value = 0
        SetStatusText("Completed")
    End Sub
End Class
 
Share this answer
 
v2
Comments
Blutfaust 9-Jul-14 15:10pm    
So you think the TO is still waiting for a solution? After 1,5 years?
Florin Pănescu 10-Jul-14 5:17am    
@Blutfaust you right, this is my first answer here, maybe this solution for him is not important anymore, but maybe for other people that searching for this solution is important.
Sorry my bad english.
Have you set a breakpoint to see if it's being called ? If it is, you could try Application.DoEvents, if the issue is just that a paint event is needed.
 
Share this answer
 
Hi,

Maybe a little late but I got things working quite easily.

First of all, I would suggest to rewrite your event handler line like this:
VB
AddHandler MyZip.ExtractProgress, AddressOf Zip_ExtractProgress
You just want to point the event to the sub Zip_ExtractProgress.

Next, you'll need to define the type of verbose info that's provided by the event.

So you would have:

VB
Private Sub Extract_Zipfile(ByVal sourcePath As String, ByVal targetPath as String)
    Dim fileToExtract as ZipFile

    fileToExtract = ZipFile.Read(sourcePath)
    With fileToExtract
        .UseUnicodeAsdNecessary = True 'To extract files with unicode chars
        AddHandler .ExtractProgress, AddressOf Zip_ExtractProgress 'The new addhandler mentioned above

        'Write code to prefetch some info, like the total uncompressed size.
        'You won't need to calculate the number of files, just use fileToExtract.Entries.Count

        'The actual extracting
        fileToExtract.ExtractAll(ExtractTo, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
    End With
End Sub

Private Sub Zip_ExtractProgress(ByVal sender As Object, ByVal e As ExtractProgressEventArgs)
    'You'll have to experiment somewhat using the smart suggest lists...
    If e.EventType = ZipProgressEventType.Extracting_BeforeExtractEntry then
        'Update your labels, progress bars, ...
        pgbFileExtractProgress.Maximum = 100
        'Better calculate the progressbar value.
        pgbFileExtractProgress.Value = Convert.ToInt16((e.BytesTransferred / e.TotalBytesToTransfer)*

    'Use a couple of ElseIfs to catch other kinds of events
    End If

    Application.DoEvents() 'Allows your forms to display the updated values.
End Sub
That should do the trick...
 
Share this answer
 
Comments
Dave Kreskowiak 25-Jan-17 10:09am    
4 years is "a little late"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900