none
Lib for unzip with event to info completed unzip RRS feed

  • Question

  • Hello,

    I need create a aplication for unzip a file .zip.

    i need use a .Net Framework 3.5 and VB.NET

    What lib i can use to this

    i need a lib with event to info unzip completed finished.

    Please, help me

    Thank you

    Friday, February 28, 2020 7:01 PM

All replies

  • and to know the unzip is finished.

    Thank you

    Friday, February 28, 2020 8:29 PM
  • To know when finished use a Delegate and event as per below which I altered from the link Castorix31 provided. There is a "but", the code requires a higher framework than 3.5 for asynchronous code I have below where async and await don't exist. To downgrade there are two paths, place code in ExtractAllAsync into DoWork of a BackGroundWorker or remove the Async key words, Await ke words and comment out Await Task.Run and End Sub)

    Enhancements

    • Knows how to handle when a file exists which the original code used try/catch
    • Ability to know when 100 percent complete
    • Provides count of extracted
    • Shows extracted in ListBox 
    Imports System.IO
    Imports System.IO.Compression
    
    ''' <summary>
    ''' Requires 1 button, 1 list box and 1 progress bar
    ''' </summary>
    Public Class Form1
        Public Event ZipEventHandler As ZipEventHandler
        Private ExtractedCount As Integer = 0
    
    
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ExtractedCount = 0
            ProgressBar1.Value = 0
            Await Task.Delay(100)
            Button1.Enabled = False
            ListBox1.Items.Clear()
    
            Await ExtractAllAsync("c:\data\data.zip", "c:\data\testextract", ProgressBar1)
            Button1.Enabled = True
        End Sub
    
        Private Sub Form1_TheFolderEventHandler(sender As Object, e As ZipArgs) _
            Handles Me.ZipEventHandler
    
            'Await Task.Delay(100)
            'Console.WriteLine(e.Extracted)
            If e.Extracted Then
                ExtractedCount += 1
                Invoke(Sub() ListBox1.Items.Add(e.FileName))
            End If
    
            If e.PercentDone = 100 Then
                MessageBox.Show($"Extracted {ExtractedCount} files.")
            End If
        End Sub
    
        Private Async Function ExtractAllAsync(zipFilePath As String, extractPath As String, progress As ProgressBar) As Task
            Try
                Dim extractCount As Integer
                Dim skipCount As Integer
                Dim errorCount As Integer
    
                Using archive As ZipArchive = ZipFile.OpenRead(zipFilePath)
    
                    progress.Minimum = 0
                    progress.Maximum = archive.Entries.Count
                    Dim extracted As Boolean = False
    
                    Await Task.Run(Sub()
    
                                       Dim count As Integer = archive.Entries.Count
                                       Dim currentFileName As String = ""
    
                                       For index As Integer = 0 To count - 1
                                           Try
                                               Dim entry As ZipArchiveEntry = archive.Entries(index)
                                               currentFileName = entry.FullName
                                               If Not File.Exists(Path.Combine(extractPath, entry.FullName)) Then
                                                   entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
                                                   extractCount += 1
                                                   extracted = True
                                               Else
                                                   extracted = False
                                               End If
    
                                           Catch ioex As IOException
                                               If ioex.Message.EndsWith("already exists.") Then
                                                   skipCount += 1
                                               Else
                                                   errorCount += 1
                                               End If
                                           Catch ex As Exception
                                               errorCount += 1
                                           End Try
    
                                           Invoke(Sub() progress.Value += 1)
                                           '
                                           ' Pass on file name without path and percent done
                                           '
                                           'Console.WriteLine($"{extracted} - {currentFileName}")
                                           RaiseEvent ZipEventHandler(Me,
                                                New ZipArgs(
                                                    currentFileName,
                                                    ProgressBar1.Value.PercentageOf(count),
                                                    extracted))
    
                                       Next
                                       '
                                   End Sub)
                End Using
    
                'MessageBox.Show(String.Format("Extracted {0} files, skipping {1} because 
                'they exist, with {2} errors.", extractCount, skipCount, errorCount), 
                '"Extraction Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error Opening Zip File", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Function
    
    
    End Class
    #Region "Place in a code module"
    Public Module Extensions
        <Runtime.CompilerServices.Extension>
        Public Function PercentageOf(sender As Integer, pTotalValue As Integer) As Integer
            Return sender * 100 \ pTotalValue
        End Function
    End Module
    #End Region
    
    #Region "Place these in a class file"
    Public Class ZipArgs
        Inherits EventArgs
    
        Private mFileName As String
        Private mPercentDone As Integer
        Private mExtracted As Boolean
        Public Sub New(FileName As String, PercentDone As Integer, Extracted As Boolean)
            mFileName = FileName
            mPercentDone = PercentDone
            mExtracted = Extracted
        End Sub
        Public ReadOnly Property FileName As String
            Get
                Return mFileName
            End Get
        End Property
        Public ReadOnly Property PercentDone As Integer
            Get
                Return mPercentDone
            End Get
        End Property
        Public ReadOnly Property Extracted() As Boolean
            Get
                Return mExtracted
            End Get
        End Property
    End Class
    Public Delegate Sub ZipEventHandler(sender As Object, e As ZipArgs)
    #End Region
    
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, February 29, 2020 1:06 PM
    Moderator
  • Hello, but 

    System.IO.Compression

    works in .Net Framework 3.5 ?

    Thank you

    Monday, March 2, 2020 10:33 AM
  • Hello, but 

    System.IO.Compression

    works in .Net Framework 3.5 ?

    Thank you

    I just checked, no it will not work with 3.5, is there any reason why you can't use Framework 4.5 ? Unless there is a constraint all you need to do is change to 4.5 and if 4.5 is not installed get it here https://www.microsoft.com/en-us/download/details.aspx?id=30653

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 2, 2020 11:14 AM
    Moderator
  • If you cannot use System.IO.Compression,

    you can use the Shell

    For example :

    ' Change Zip file and destination
    
    Public Sub BackgroundTask()
        ' Add reference : Microsoft Shell Controls and Automation
        ' Add : Imports Shell32
        Dim objShell As Shell32.Shell = New Shell32.Shell()
        Dim sZipPath As String = "E:\Test\Test.zip"
        Dim sOutputPath As String = "E:\Test2"
        Dim objOutput As Shell32.Folder = objShell.[NameSpace](sOutputPath)
        Dim objPathFolder As Shell32.Folder = objShell.[NameSpace](sZipPath)
        Dim nNbItems As Integer = 0
        For Each objFolderItem As Shell32.FolderItem2 In objPathFolder.Items()
            ' Console.WriteLine("Name : {0}", objFolderItem.Name)
            objOutput.CopyHere(objFolderItem, 256 Or 16)
            nNbItems += 1
        Next
        MessageBox.Show(String.Format("Extraction done" + vbCrLf + "Number of extracted items : {0}", nNbItems), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Dim wth As New System.Threading.Thread(AddressOf BackgroundTask)
    wth.IsBackground = True
    wth.Name = "Background Task"
    wth.SetApartmentState(Threading.ApartmentState.STA)
    wth.Start()

    Monday, March 2, 2020 2:13 PM
  • Hello,

    Thank you very mutch for all
    Tuesday, March 3, 2020 6:46 PM