c#压缩和解压缩

--1.ClassBinary

 1 public class ClassBinary
 2 {
 3     public ClassBinary()
 4     {
 5         //
 6         // TODO: 在此处添加构造函数逻辑
 7         //
 8     }
 9     /// <summary>
10     /// 压缩
11     /// </summary>
12     /// <param name="ds"></param>
13     /// <returns></returns>
14     public byte[] CompressToBytes(DataSet ds)
15     {
16         DataSetSurrogate dss = new DataSetSurrogate(ds);
17         BinaryFormatter ser = new BinaryFormatter();
18         MemoryStream ms = new MemoryStream();
19         ser.Serialize(ms, dss);
20         byte[] buffer = ms.ToArray();
21         byte[] zipBuffer = new CompressionHelper(CompressionLevel.BestSpeed).CompressToBytes(buffer);
22 
23         return zipBuffer;
24     }
25     /// <summary>
26     /// 解压
27     /// </summary>
28     /// <param name="zipBuffer"></param>
29     /// <returns></returns>
30     public DataSet DecompressToBytes(byte[] zipBuffer)
31     {
32         if (zipBuffer == null) return null;
33         byte[] buffer = new CompressionHelper().DecompressToBytes(zipBuffer);
34         BinaryFormatter ser = new BinaryFormatter();
35         DataSetSurrogate dss = ser.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
36         DataSet dsZip = dss.ConvertToDataSet();
37 
38         return dsZip;
39     }
40 
41 }

2.使用

1、压缩
DataSet ds = new DataSet();//假设的ds
byte[] byte = new ClassBinary().CompressToBytes(ds );

2、解压缩
DataSet ds2 =new ClassBinary().DecompressToBytes(byte);

 

posted @ 2016-04-12 14:50  zzfon  阅读(472)  评论(0编辑  收藏  举报