Compress DataSet with .net 2.0

來源:互聯網
上載者:User
NET Framework 2.0 版中新增GZipStream 類,對了就是它了!
啥也不說了,咣咣的,別整那些沒用的,代碼說明一切!

 

 1 ''' <summary>
 2     ''' 
 3     ''' </summary>
 4     ''' <param name="data"></param>
 5     ''' <returns></returns>
 6     ''' <remarks></remarks>
 7     Public Shared Function CompressDataSet(ByVal data As DataSet) As Byte()
 8 
 9 
10         Try
11 
12             Dim bf As New BinaryFormatter
13             Using ms As MemoryStream = New MemoryStream
14 
15                 bf.Serialize(ms, data)
16                 Dim b() As Byte = ms.GetBuffer()
17 
18                 Dim output As New MemoryStream
19                 Dim gzip As New GZipStream(output, CompressionMode.Compress, True)
20 
21                 gzip.Write(b, 0, b.Length)
22                 gzip.Close()
23 
24                 Return output.ToArray
25 
26 
27             End Using
28 
29 
30 
31         Catch ex As ApplicationException
32             MessageBox.Show(ex.Message, "An Error occured during compression", MessageBoxButtons.OK, MessageBoxIcon.Error)
33         Finally
34 
35         End Try
36 
37     End Function
38 
39     ''' <summary>
40     ''' 
41     ''' </summary>
42     ''' <param name="data"></param>
43     ''' <returns></returns>
44     ''' <remarks></remarks>
45     Public Shared Function DecompressDataSet(ByVal data As Byte()) As DataSet
46 
47 
48         Try
49 
50             Dim input As New MemoryStream
51             input.Write(data, 0, data.Length)
52             input.Position = 0
53             Dim gzip As New GZipStream(input, 0, True)
54             Dim output As New MemoryStream
55             Dim buff As Byte() = New Byte(4096) {}
56             Dim read As Integer = -1
57             read = gzip.Read(buff, 0, buff.Length)
58             Do While (read > 0)
59                 output.Write(buff, 0, read)
60                 read = gzip.Read(buff, 0, buff.Length)
61             Loop
62             gzip.Close()
63             Dim result() As Byte = output.ToArray
64 
65 
66             Dim bf As New BinaryFormatter
67             Using ms As MemoryStream = New MemoryStream(result)
68                 Return DirectCast(bf.Deserialize(ms), DataSet)
69             End Using
70 
71 
72         Catch ex As ApplicationException
73             MessageBox.Show(ex.Message, "An Error occured during decompression", MessageBoxButtons.OK, MessageBoxIcon.Error)
74         Finally
75 
76         End Try
77 
78     End Function


Sample

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.