Hi all,
I'm trying to display the total extraction progress of a zip file as below.
This gives me the total file size of all uncompressed files in the zip:
For Each backup In zip
TotalSize = backup.UncompressedSize + TotalSize
Next
I'm then using this to get the current bytes transfered:
TranferTotal = e.BytesTransferred + TranferTotal
The problem is this value resets after each file is extracted from the zip. I was expecting the above to "tally up" the total so I could use it for the progress bar but it seems to still reset after each item?
Also, I'm using a global varible for the transfer total, if there a better method to transfer this information to the StepEntryProgress?
The reason I'm doing this is because the DotNetZip Library does not support multiple extraction in this way, so I need to tally up in this way.
Code:
Imports Ionic.Zip
Imports System.Threading
Imports System.ComponentModel
Public Class Form1
Dim TotalSize = 0
Private _zipBGWorker As System.ComponentModel.BackgroundWorker
Private Delegate Sub ZipProgress(ByVal e As ExtractProgressEventArgs)
Private Delegate Sub ExtractEntryProgress(ByVal e As ExtractProgressEventArgs)
Private Sub BtnUnzip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUnzip.Click
MyExtract()
End Sub
Private Sub MyExtract()
Dim args(2) As String
args(0) = "C:\Test\Test2.zip"
args(1) = "C:\Test\ZipTestExtract\"
_zipBGWorker = New System.ComponentModel.BackgroundWorker()
_zipBGWorker.WorkerSupportsCancellation = False
_zipBGWorker.WorkerReportsProgress = False
AddHandler Me._zipBGWorker.DoWork, New DoWorkEventHandler(AddressOf Me.UnzipFile)
_zipBGWorker.RunWorkerAsync(args)
End Sub
Private Sub UnzipFile(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim extractCancelled As Boolean = False
Dim args() As String = e.Argument
Dim ZipToUnpack As String = args(0)
Dim extractDir As String = args(1)
Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
AddHandler zip.ExtractProgress, New EventHandler(Of ExtractProgressEventArgs)(AddressOf Me.zip_ExtractProgress)
For Each backup In zip
TotalSize = backup.UncompressedSize + TotalSize
Next
For Each backup In zip
backup.Extract(extractDir, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
Next
End Using
End Sub
Private Sub zip_ExtractProgress(ByVal sender As Object, ByVal e As ExtractProgressEventArgs)
Me.StepEntryProgress(e)
End Sub
Private Sub StepEntryProgress(ByVal e As ExtractProgressEventArgs)
Dim TranferTotal As Integer
TranferTotal = e.BytesTransferred + TranferTotal
If Me.ProgressBar1.InvokeRequired Then
Me.ProgressBar1.Invoke(New ExtractEntryProgress(AddressOf Me.StepEntryProgress), New Object() {e})
Else
Me.ProgressBar1.Maximum = 100
Me.ProgressBar1.Value = 100 * TranferTotal / TotalSize
'Me.ProgressBar1.Value = 100 * e.BytesTransferred / e.TotalBytesToTransfer
Me.LblStatus.Text = CStr(100 * (e.BytesTransferred / e.TotalBytesToTransfer).ToString("#,##0.00")) & "% Complete"
End If
End Sub
End Class
many thanks.