שתף באמצעות


Unzip/Extract with DotNetZip Library Showing Progress - Out Of Range Exception

Question

Friday, December 18, 2015 10:27 PM

Hi everyone,

I've been testing the DotNetLibrary for the last days using the examples provided on the official project website and available on the Internet.

I managed to zip files bigger than 4GB, which was my goal in first place, but now I am getting an Out Of Range Exception regarding the ProgressBar.

This is the code I am using to extract the Zip file:

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


    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As

System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged


        ProgressBar1.Maximum = CInt(CurrentCount)
        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) ' I think this is not necessary, right?
        If Me.InvokeRequired Then
            Me.Invoke(New SetStatusTextInvoker(AddressOf SetStatusText), Text)
        Else
            lblStatus.Text = Text
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As

System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ExtractZip("d:\lixo\testeZIPgrande.zip", "d:\lixo\testeZIPgrandeExtraido\")
    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("d:\lixo\testeZIPgrandeExtraido\",

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)

'overwrite
            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
        Dim Porcentagem As Integer
        System.Windows.Forms.Application.DoEvents()
        If Total <> e.TotalBytesToTransfer Then
            Porcentagem = ProgressBar1.Value / ProgressBar1.Maximum * 100 ' get percentage
            SetStatusText(Porcentagem & "%" & "  >  " & 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

I have a button on my form that will call:

BackgroundWorker1.RunWorkerAsync()

And this is the error I'm getting:

Error Screenshot - http://i.imgur.com/40WVCsx.jpg

If I comment out the tow lines on BackgroundWorker1_ProcessChanged Sub like this...

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As

System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged


        'ProgressBar1.Maximum = CInt(CurrentCount)
        'ProgressBar1.Value = e.ProgressPercentage

    End Sub

...The extraction will work, but I will have no progress bar.

I know (think?) the problem is on getting the "quantity" (?) of the files inside the Zip file, to give the appropriate progressbar.maximum value but i can't work out how to fix this.

I wanted to keep as simple as possible, I wanted to use the examples provided from DotNetZip project page, but I can't manage to make them working :(

Also, I saw some example when I was Googling that could work (or not) but I never tried them because I want this to work with multi-thread to avoid freezing the UI.

Any help is appreciated.

Thanks

PS: I can't have links or images posted here yet due to: "Body text cannot contain images or links until we are able to verify your account."

All replies (1)

Saturday, December 19, 2015 2:19 AM ✅Answered | 1 vote

 I have already posted an answer to your question at the link below so,  instead of retyping my reply,  you can read it there.

Unzip/Extract with DotNetZip Library Showing Progress

If you say it can`t be done then i`ll try it