利用 .net 的 System.IO.Compression 做檔案的壓縮

這篇跟前一篇不一樣的是
只能做檔案的壓縮與解壓的動作
不用參考第三方元件
中文檔也OK

如果 .NET Framework 4.5





.NET Framework 4From http://msdn.microsoft.com/zh-tw/library/system.io.compression.gzipstream(v=vs.100).aspx

Code Snip 
Imports System.IO
Imports System.IO.Compression

Public Class Form1
    Const DirPath As String = "C:\T1"
    Private Sub btnFileZip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileZip.Click
        ' Path to directory of files to compress.
        Dim di As DirectoryInfo = New DirectoryInfo(DirPath)
        ' Compress the directory's files.
        For Each fi As FileInfo In di.GetFiles()
            Compress(fi)
        Next
    End Sub

    Private Sub btnFileUnzip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileUnzip.Click
        ' Path to directory of files to compress.
        Dim di As DirectoryInfo = New DirectoryInfo(DirPath)
        ' Decompress all *.gz files in the directory.
        For Each fi As FileInfo In di.GetFiles("*.gz")
            Decompress(fi)
        Next
    End Sub

    ' Method to compress.
    Private Sub Compress(ByVal fi As FileInfo)
        ' Get the stream of the source file.
        Using inFile As FileStream = fi.OpenRead()
            ' Compressing:
            ' Prevent compressing hidden and already compressed files.
            If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) _
                <> FileAttributes.Hidden And fi.Extension <> ".gz" Then
                ' Create the compressed file.
                Using outFile As FileStream = File.Create(fi.FullName + ".gz")
                    Using Compress As GZipStream = _
                     New GZipStream(outFile, CompressionMode.Compress)
                        ' Copy the source file into the compression stream.
                        inFile.CopyTo(Compress)
                        ListBox1.Items.Add(fi.Name & "--" & fi.Length.ToString() & "--" & outFile.Length.ToString())
                    End Using
                End Using
            End If
        End Using
    End Sub

    ' Method to decompress.
    Private Sub Decompress(ByVal fi As FileInfo)
        ' Get the stream of the source file.
        Using inFile As FileStream = fi.OpenRead()
            ' Get orignial file extension, for example "doc" from report.doc.gz.
            Dim curFile As String = fi.FullName
            Dim origName = curFile.Remove(curFile.Length - fi.Extension.Length)
            ' Create the decompressed file.
            Using outFile As FileStream = File.Create(origName)
                Using Decompress As GZipStream = New GZipStream(inFile, _
                 CompressionMode.Decompress)
                    ' Copy the decompression stream
                    ' into the output file.
                    Decompress.CopyTo(outFile)
                    ListBox1.Items.Add("Decompressed: " & fi.Name)
                End Using
            End Using
        End Using
    End Sub
End Class

沒有留言:

張貼留言