.NET2.0壓縮文件

    Public Function AddZipFile(ByVal srcFileName As String, ByVal zipFileName As String, ByRef ERRORStr As String) As Boolean
        ERRORStr = ""
        If Not File.Exists(srcFileName) Then
            ERRORStr = "WARN|原文件[" + srcFileName + "]路徑不正確,請查正。"
            Return False
        End If
        Dim result As Boolean = False
        Dim fs As FileStream = Nothing
        Dim output As FileStream = Nothing
        Dim zipStream As GZipStream = Nothing
        Try
            fs = New FileStream(srcFileName, FileMode.Open, FileAccess.Read)
            Dim buffer As Byte() = New Byte(fs.Length) {}
            fs.Read(buffer, 0, buffer.Length)
            fs.Close()
            If File.Exists(zipFileName) Then
                Try
                    File.Delete(zipFileName)
                Catch ex As Exception
                    ERRORStr = "WARN|壓縮檔[" + zipFileName + "]已存在且刪除失敗,請查正。"
                    Return False
                End Try
            End If
            output = File.Create(zipFileName)
            zipStream = New GZipStream(output, CompressionMode.Compress, False)
            zipStream.Write(buffer, 0, buffer.Length)
            result = True

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

你可能感兴趣的:(.NET2.0壓縮文件)