Compress DataSet with .net 2.0

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(input0True)
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


posted @ 2006-11-01 11:40 MasterCai 阅读(1346) 评论(10)  编辑 收藏 网摘 所属分类: VB.Net.net Framework

  回复  引用  查看    
#1楼2006-11-01 11:42 | neuhawk      
对于数据量小,是否不压缩更快?
  回复  引用    
#2楼2006-11-01 11:54 | e商[未注册用户]
学习中。。
  回复  引用  查看    
#3楼2006-11-01 12:03 | microshot      
用过,速度差不了多少,除非记录过10w
  回复  引用  查看    
#4楼[楼主]2006-11-01 12:16 | MasterCai      
有时候,不光要考虑速度的问题,还要考虑数据流量的问题!
比如采用Webservice,用DataSet作为数据传输,如果Client连接人多的话,性能很明显,压缩的更好!

  回复  引用  查看    
#5楼2006-11-01 12:22 | ∈鱼杆      
不错,有淘到不错的代码
  回复  引用    
#6楼2006-11-01 13:34 | 美娃~西西[未注册用户]
不知道net 1.0中有没有这个类。


不错的在线个人知识管理网站
*** 美娃~西西
大家看看呀

  回复  引用    
#7楼2006-11-01 15:24 | 天天影院[未注册用户]
很细致
  回复  引用    
#9楼2006-12-08 10:10 | Hello[匿名][未注册用户]
例子中的程序是错误的,压缩可以,解压缩不可以!
  回复  引用  查看    
#10楼[楼主]2007-06-01 18:06 | MasterCai      
例子中的错误已经修正!



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 546557




相关文章:

相关链接: