ASP.NET 2.0 的 壓縮(Compress)、解壓縮(Decompress)----System.IO.Compression 命名空間

在ASP.NET實現「線上壓縮」與「解壓縮」。
微軟官方範例與相關文章......System.IO.Compression 命名空間

ASP.NET(從v2.0起)如何壓縮、解壓縮檔案呢?

我找到微軟的一些範例,僅供參考:

System.IO.Compression 命名空間
System.IO.Compression 命名空間包含提供資料流基本壓縮和解壓縮服務的類別。

http://msdn.microsoft.com/zh-tw/library/system.io.compression.aspx
下列程式碼範例會示範如何使用 GZipStream 類別,以壓縮及解壓縮檔案。

 

Visual Basic語法(C#語法請看上面的URL,在此不列出)

....................................................................................................................

Imports System
Imports System.IO
Imports System.IO.Compression
 
Public Class GZipTest
    Shared msg As String
    Private Const buffer_size As Integer = 100
 
    Public Shared Function ReadAllBytesFromStream(ByVal stream As Stream, ByVal buffer() As Byte) As Integer
        ' Use this method is used to read all bytes from a stream.
        Dim offset As Integer = 0
        Dim totalCount As Integer = 0
        While True
            Dim bytesRead As Integer = stream.Read(buffer, offset, buffer_size)
            If bytesRead = 0 Then
                Exit While
            End If
            offset += bytesRead
            totalCount += bytesRead
        End While
        Return totalCount
    End Function 'ReadAllBytesFromStream結束
 
    Public Shared Function CompareData(ByVal buf1() As Byte, ByVal len1 As Integer, ByVal buf2() As Byte, ByVal len2 As Integer) As Boolean
        ' 兩個Buffer之間,作比較。Use this method to compare data from two different buffers.
        If len1 <> len2 Then
            msg = "Number of bytes in two buffer are different" & len1 & ":" & len2
            MsgBox(msg)
            Return False
        End If
 
        Dim i As Integer
        For i = 0 To len1 - 1
            If buf1(i) <> buf2(i) Then
                msg = "byte " & i & " is different " & buf1(i) & "|" & buf2(i)
                MsgBox(msg)
                Return False
            End If
        Next i
        msg = "All bytes compare(比較完畢~)."
        MsgBox(msg)
        Return True
    End Function 'CompareData結束
 
 
    Public Shared Sub GZipCompressDecompress(ByVal filename As String)
        msg = "Test compression and decompression on file " & filename
        MsgBox(msg)
 
        Dim infile As FileStream
        Try
            ' Open the file as a FileStream object.
            infile = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim buffer(infile.Length - 1) As Byte
            ' Read the file to ensure it is readable.
            Dim count As Integer = infile.Read(buffer, 0, buffer.Length)
            If count <> buffer.Length Then
                infile.Close()
                msg = "Test Failed: Unable to read data from file(讀不到檔案!)"
                MsgBox(msg)
                Return
            End If
            infile.Close()
            Dim ms As New MemoryStream()
            ' Use the newly created memory stream for the compressed data.壓縮檔案
            Dim compressedzipStream As New GZipStream(ms, CompressionMode.Compress, True)
            compressedzipStream.Write(buffer, 0, buffer.Length)
            ' Close the stream.
            compressedzipStream.Close()
            msg = "原本的Original size: " & buffer.Length & ",  壓縮後Compressed size: " & ms.Length
            MsgBox(msg)

            ' ==============================================
            ' Reset the memory stream position to begin decompression. 解壓縮!!
            ms.Position = 0
            Dim zipStream As New GZipStream(ms, CompressionMode.Decompress)
            Dim decompressedBuffer(buffer.Length + buffer_size) As Byte
            ' Use the ReadAllBytesFromStream to read the stream.
            Dim totalCount As Integer = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer)
            msg = "Decompressed(解壓縮) " & totalCount & " bytes"
            MsgBox(msg)
 
            If Not GZipTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) Then
                msg = "Error. The two buffers did not compare."
                MsgBox(msg)
            End If
            zipStream.Close()
        Catch e As Exception
            msg = "Error: The file being read contains invalid data."
            MsgBox(msg)
        End Try
    End Sub 'GZipCompressDecompress結束
 
    Public Shared Sub Main(ByVal args() As String)      '主程式
        Dim usageText As String = "Usage: GZIPTEST <inputfilename>"
        'If no file name is specified, write usage text.
        If args.Length = 0 Then
               Console.WriteLine(usageText)
        Else
            If File.Exists(args(0)) Then
                   GZipCompressDecompress(args(0))
            End If
        End If
    End Sub 'Main結束
End Class 'GZipTest 

 

另外,我在估狗上也找到一些不錯的文章,一併列出來給大家參考:

http://aspalliance.com/1348_Understanding_Compression_and_Decompression_in_ASPNET_20.all
http://211.147.225.34/gate/big5/dotnet.chinaitlab.com/CSharp/378794.html
http://blog.csdn.net/21aspnet/archive/2007/06/13/1649810.aspx

小喵大大的文章:使用自訂的類別透過SharpZip執行Zip壓縮、解壓縮(VB.NET)
http://blog.blueshop.com.tw/topcat/archive/2008/02/04/54267.aspx

 

 

 
 

我將思想傳授他人, 他人之所得,亦無損於我之所有;

猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson

寫信給我--  mis2000lab (at) yahoo.com.台灣  或是  school (at) mis2000lab.net

................   facebook社團   https://www.facebook.com/mis2000lab   ......................

................   Google+   https://plus.google.com/100202398389206570368/posts ........

................  YouTube (ASP.NET) 線上教學影片  http://goo.gl/rGLocQ

*********************************************************************************************

*** 第一天 的 ASP.NET MVC線上課程 免費看(5.5小時)*** 

************************************************************(歡迎索取,免費申請)*****

 

ASP.NET遠距教學、線上課程(Web Form + MVC)第二門 課程「四折」-- 以MVC課程作為優惠。

第一天完整課程,提供 "完整" 試聽。  如不滿意 全額退費!

 

 

Blog文章 "附的範例" 無法下載,請看 這裡 ...... https://dotblogs.com.tw/mis2000lab/2016/03/14/2008_2015_mis2000lab_sample_download

請看我們的「售後服務」範圍(嚴格認定)

......................................................................................................................................................

...................................................................................................................................................... 

[遠距教學、教學影片] ASP.NET (Web Form) 課程 上線了!MIS2000Lab.主講

事先錄製好的影片,並非上課時側錄!   觀看影片時,有如我「一對一」跟您面對面講課

 

    MIS2000 Lab.  線上教學影片(YouTube) **免費觀賞**