DotNetZip Progress帮助 [英] DotNetZip Progress Help

查看:98
本文介绍了DotNetZip Progress帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当我尝试打开拉链时,它不会更新进度条,欢迎任何帮助。



For some reason when I try to unpack a zip it does not update the progress bar any help would be welcomed.

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

推荐答案

试试这段代码,看看是否有帮助,我只使用一个ProgressBar。



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


您是否设置了断点以查看它是否被调用?如果是,你可以尝试Application.DoEvents,如果问题只是需要一个paint事件。
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.


你好,



也许有点晚了但我的工作很轻松。



首先,我建议你重写这样的事件处理程序:

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:
AddHandler MyZip.ExtractProgress, AddressOf Zip_ExtractProgress

你只想将事件指向子 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:

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

那个守ld做的伎俩......

That should do the trick...


这篇关于DotNetZip Progress帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆