
Imports System.IO
Imports System.IO.Compression
Imports System.Text

Public Class ClassUnzip
    Public Event ProgressChanged(ByVal actprogress As Integer, ByVal overall As Integer, ByVal path As String)
    Public Event UnzipFinished(ByVal extractCount As Integer, ByVal errors As Integer)

    Public Sub ExtractAllAsync(zipFilePath As String, extractPath As String)
        Try
            Dim extractCount, errorCount, allFiles As Integer
            Using archive As ZipArchive = ZipFile.OpenRead(zipFilePath)

                allFiles = archive.Entries.Count


                Dim count As Integer = archive.Entries.Count
                For i As Integer = 0 To count - 1
                    Try
                        Dim entry As ZipArchiveEntry = archive.Entries(i)
                        Dim pfad As String = entry.FullName.Replace("/", "\")
                        pfad = correctpath(pfad)
                        If My.Computer.FileSystem.FileExists(Path.Combine(extractPath, pfad)) Then
                            My.Computer.FileSystem.DeleteFile(Path.Combine(extractPath, pfad))
                        End If
                        If pfad.EndsWith("\") Then
                            My.Computer.FileSystem.CreateDirectory(Path.Combine(extractPath, pfad))
                        Else
                            entry.ExtractToFile(Path.Combine(extractPath, pfad))
                        End If
                        extractCount += 1
                        RaiseEvent ProgressChanged(extractCount, allFiles, pfad)
                    Catch ioex As IOException
                        errorCount += 1
                    Catch ex As Exception
                        errorCount += 1
                    End Try
                    'System.Threading.Thread.Sleep(100) 'might want delay to see progress animation better
                Next
                'System.Threading.Thread.Sleep(100)
            End Using
            RaiseEvent UnzipFinished(extractCount, errorCount)
            'MessageBox.Show(String.Format("Extracted {0} files, skipping {1} because they exist, with {2} errors.", extractCount, skipCount, errorCount), "Extraction Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
            'Progress.Value = 0
        Catch ex As Exception
            'MessageBox.Show(ex.Message, "Error Opening Zip File", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Function correctpath(ByVal path As String) As String
        Dim utf8Encoding As Encoding = Encoding.Default
        Dim unicodeEncoding As Encoding = System.Text.Encoding.GetEncoding(852)
        Dim bytes() As Byte
        Dim chars(50) As Char
        Dim index As Integer
        bytes = utf8Encoding.GetBytes(path)

        Dim count As Integer = unicodeEncoding.GetCharCount(bytes)
        If count + index >= chars.Length Then
            Array.Resize(chars, chars.Length + 50)
        End If
        Dim written As Integer = unicodeEncoding.GetChars(bytes, 0, bytes.Length, chars, index)
        index = index + written
        Dim decodedString As New String(chars, 0, index)
        Return decodedString
    End Function
End Class
