Compress DataSet with .net 2.0
NET Framework 2.0 版中新增GZipStream 类,对了就是它了!
啥也不说了,咣咣的,别整那些没用的,代码说明一切!
Sample
啥也不说了,咣咣的,别整那些没用的,代码说明一切!
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
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