• 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);

  • 相关阅读:
    第03次作业-栈和队列
    第02次作业-线性表
    Data-Structure01-绪论
    c语言第二次实验报告
    C语言第一次实验报告
    KD-tree讲解
    AAAA、
    清北学堂 day one
    生长,开始记录!
    Linux命令(待完善)
  • 原文地址:https://www.cnblogs.com/zzfon/p/5382686.html
Copyright © 2020-2023  润新知