.NET2.0解壓縮文件

Imports System.IO.Compression

    Public Function ExtractZipFile(ByVal ExtractFileName As String, ByVal zipFileByte As Byte(), ByRef ERRORStr As String) As Boolean
        ERRORStr = ""
        Dim result As Boolean = False
        Dim fs As FileStream = Nothing
        Dim sourceStream As MemoryStream = Nothing
        Dim zipStream As GZipStream = Nothing
        Dim desFileStream As FileStream = Nothing
        Try
            If File.Exists(ExtractFileName) Then
                Try
                    File.Delete(ExtractFileName)
                Catch ex As Exception
                    ERRORStr = "WARN|壓縮檔[" + ExtractFileName + "]已存在且刪除失敗,請查正。"
                    Return False
                End Try
            End If
            sourceStream = New MemoryStream(zipFileByte)
            zipStream = New GZipStream(sourceStream, CompressionMode.Decompress, True)
            Dim quartetBuffer As Byte() = New Byte(4) {}
            Dim position As Integer = sourceStream.Length - 4
            sourceStream.Position = position
            sourceStream.Read(quartetBuffer, 0, 4)
            sourceStream.Position = 0
            Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)
            Dim buffer As Byte() = New Byte(checkLength + 100) {}
            Dim offset As Integer = 0
            Dim total As Integer = 0
            While (True)
                Dim bytesRead As Integer = zipStream.Read(buffer, offset, 100)
                If bytesRead = 0 Then
                    Exit While
                End If
                offset += bytesRead
                total += bytesRead
            End While

            desFileStream = New FileStream(ExtractFileName, FileMode.Create)
            desFileStream.Write(buffer, 0, total)
            desFileStream.Flush()

            result = True

        Catch ex As Exception
            ERRORStr = "ERROR|系統在函數[ExtractZipFile]中發生錯誤:" + ex.ToString
            result = False
        Finally
            If Not zipStream Is Nothing Then
                zipStream.Flush()
                zipStream.Close()
            End If
            If Not desFileStream Is Nothing Then
                desFileStream.Flush()
                desFileStream.Close()
            End If
            If Not sourceStream Is Nothing Then
                sourceStream.Close()
            End If
        End Try
        Return result
    End Function 

你可能感兴趣的:(exception,.net,function,Integer,buffer,byte)