使用自訂的類別透過SharpZip執行Zip壓縮、解壓縮(VB.NET)

小喵自己在撰寫一個類別將SharpZip包裝起來,只提供三個簡單的Function,提供以下的功能:
ZipDir(SourceDir,TargetFile):壓縮資料夾
ZipFile(SourceFile,TargetFile):壓縮單一檔案
UnZipFile(SourceFile,TargetDir):解壓縮到指定資料夾
這樣小喵就能夠用較為直觀的方式來壓縮、解壓縮。

小喵最近遇到一個狀況,小喵需要使用者上傳一個檔案,上傳後再由小喵的系統接手處理該檔案,但是如果上傳的檔案太大,會造成兩個影響:1.佔用頻寬。2.使用者上傳時間過久。

為了解決這樣的問題,於是小喵希望使用者上傳前,透過OS內建的Zipped壓縮機制將檔案壓縮成zip檔,然後上傳後再由小喵的系統接手解壓縮,之後再來處理檔案。

那麼就會遇到一個狀況:.NET Framework裡面雖然有System.IO.Compression這個命名空間,但是他的格式是Gzip而非ZIP,因此一般透過工具或者使用OS內建的Zipped產生的檔案無法透過這個命名空間提供的方式來解壓縮。小喵再次從網路上搜尋,發現有人透過J#的命名空間System.Java.Zip來處理。但是很不幸的是從.NET Framework 2.0開始不再支援J#這個語言。因此這樣的方式也不行。U_U

因此小喵開始尋找第三方的解決方案,找到了一個SharpZipLib這個免費的元件有提供可以壓縮與解壓縮zip格式。不過該元件的官方網站請參考【http://www.icsharpcode.net/OpenSource/SharpZipLib/】。該網站也提供範例檔案、原始碼提供使用者參考與研究。小喵試用之後發現,他裡面有提供一個FastZip的類別,裡面可以快速的直行兩個動作,分別是:

  1. 壓縮某資料夾(含子目錄)成為一個zip檔
  2. 解壓縮一個zip檔到指定的目錄

但是如果想要【壓縮某單一檔案成為zip檔】,在這個FastZip類別中並沒有提供,如果要做到這個需求,必須在撰寫一些額外的程式碼來達到。這對於使用上感覺不是很直觀

於是小喵自己在撰寫一個類別將SharpZip包裝起來,只提供三個簡單的Function,提供以下的功能:

  1. ZipDir(SourceDir,TargetFile):壓縮資料夾
  2. ZipFile(SourceFile,TargetFile):壓縮單一檔案
  3. UnZipFile(SourceFile,TargetDir):解壓縮到指定資料夾

這樣小喵就能夠用較為直觀的方式來壓縮、解壓縮。相關的原始碼如下:

Imports System.IO
Imports ICSharpCode.SharpZipLib.Checksums
Imports ICSharpCode.SharpZipLib.Zip
Imports ICSharpCode.SharpZipLib.GZip
Imports System.IO.Compression


Public Class CZip
    Public Function ZipDir(ByVal SourceDir As String, ByVal TargetFile As String) As String
        '**********************************************************************************
        '  壓縮整個目錄
        '  傳入參數:
        '       SourceDir:要壓縮的資料夾(完整路徑)
        '       TargetFile:要產生的Zip檔案(完整路徑檔名)
        '**********************************************************************************
        Dim oZipDir As New ICSharpCode.SharpZipLib.Zip.FastZip
        Try
            '檢查目錄是否存在
            If Not (My.Computer.FileSystem.DirectoryExists(SourceDir)) Then
                Throw New Exception("資料夾不存在!!")
            End If
            oZipDir.CreateZip(TargetFile, SourceDir, True, "")
            Return "Success"

        Catch ex As Exception
            Throw New Exception(ex.Message)

        End Try
    End Function

    Public Function ZipFile(ByVal SourceFile As String, ByVal TargetFile As String) As String
        '**********************************************************************************
        '   壓縮單一檔案
        '**********************************************************************************
        '檢查要壓縮的檔案是否存在
        Try
            If Not My.Computer.FileSystem.FileExists(SourceFile) Then
                Throw New Exception("要壓縮的檔案【" & SourceFile & "】不存在")
            End If

            Dim zs As ZipOutputStream = New ZipOutputStream(File.Create(TargetFile))
            zs.SetLevel(9)
            Dim fn As String = SourceFile
            Dim oSF As New oFile(SourceFile)
            Dim s As FileStream = File.OpenRead(fn)
            Dim buffer As Byte() = New Byte(s.Length - 1) {}
            s.Read(buffer, 0, buffer.Length)
            'Dim Entry As New ZipEntry("nice.mdb")
            Dim Entry As New ZipEntry(oSF.FileName & "." & oSF.Exten)
            Entry.DateTime = DateTime.Now
            Entry.Size = s.Length
            s.Close()
            zs.PutNextEntry(Entry)
            zs.Write(buffer, 0, buffer.Length)
            zs.Finish()
            zs.Close()

            Return "Success"

        Catch ex As Exception
            Throw New Exception(ex.Message)

        End Try
    End Function

    Public Function UnZipFile(ByVal SourceFile As String, ByVal TargetDir As String) As String
        '**********************************************************************************
        '  解壓縮
        '  傳入參數:
        '       SourceFile:要產生的Zip檔案(完整路徑檔名)
        '       TargetFile:解壓縮後存放的路徑(完整路徑)
        '**********************************************************************************
        Dim oZip As New ICSharpCode.SharpZipLib.Zip.FastZip
        Try
            '判斷ZIP檔案是否存在
            If Not My.Computer.FileSystem.FileExists(SourceFile) Then
                Throw New Exception("要解壓縮的檔案【" & SourceFile & "】不存在,無法執行")
            End If

            oZip.ExtractZip(SourceFile, TargetDir, "")
            Return "Success"

        Catch ex As Exception
            Throw New Exception(ex.Message)

        End Try
    End Function

End Class

這裡面會用到oFile的類別,定義如下:

Public Class oFile
    Private m_FileName As String
    Private m_Path As String
    Private m_Exten As String

    Public Property FileName() As String
        Get
            Return m_FileName
        End Get
        Set(ByVal value As String)
            m_FileName = value
        End Set
    End Property

    Public Property Path() As String
        Get
            Return m_Path
        End Get
        Set(ByVal value As String)
            m_Path = Path
        End Set
    End Property

    Public Property Exten() As String
        Get
            Return m_Exten
        End Get
        Set(ByVal value As String)
            m_Exten = value
        End Set
    End Property

    Public ReadOnly Property FullFileName() As String
        Get
            Return m_Path & "\" & m_FileName & "." & m_Exten
        End Get
    End Property

    Public Sub New()

    End Sub

    Public Sub New(ByVal thisFullFileName As String)
        Dim tStr() As String = Split(thisFullFileName, "\")
        Dim Fn As String = tStr(tStr.Length - 1)
        Dim tStr2() As String = Split(Fn, ".")
        m_Exten = tStr2(1)
        m_FileName = Replace(Fn, "." & m_Exten, "")
        m_Path = Replace(thisFullFileName, "\" & Fn, "")
    End Sub

End Class

 

使用方式:
先將該專案的bin中的DeBug中的兩個dll【ICSharpCode.SharpZipLib.dll】,【MySharpZip】加入參考。
然後就可以

Dim oZip As New MySharpZip.CZip
Dim rc As String=oZip.UnZipFile("D:\Temp\abc.zip", "D:\Temp")
If rc = "Success" Then
	Me.lblMsg.Text="解壓縮成功!!"
End If

 

 


以下是簽名:


Microsoft MVP
Visual Studio and Development Technologies
(2005~2019/6) 
topcat
Blog:http://www.dotblogs.com.tw/topcat