zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    Refined Architecture阶段
    将博客搬至CSDN
    第七周学习总结
    struts2框架学习三 ajax
    Data truncation: Truncated incorrect DOUBLE value: '张利杰'
    struts2框架学习二 action对jsp传值
    决策树
    struts2框架学习1
    寒假自学第十五天
    寒假自学第十四天
  • 原文地址:https://www.cnblogs.com/zzfon/p/5382686.html
Copyright © 2011-2022 走看看