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


转载于:https://www.cnblogs.com/MasterCai/archive/2006/11/01/546557.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值