zoukankan      html  css  js  c++  java
  • 压缩解压缩传输的数据

    压缩:

      /// <summary>
            /// 把datatable数据转换成字节数组,然后压缩字节数组。
            /// </summary>
            /// <param name="data">查询得到的数据表</param>
            /// <returns>返回被压缩的字节数组</returns>
            public static byte[] GzipCompress(DataTable data)
            {
                if (data == null || data.Rows.Count <= 0)
                    return null;
                byte[] result = null;
                byte[] retbyte = null;//压缩后返回的字节数组
                try
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, data);
                        result = stream.GetBuffer();
                    }
                    if (result != null)
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
                            {
                                gZipStream.Write(result, 0, result.Length);
                                gZipStream.Close();
                            }
                            retbyte=stream.ToArray();
                        }
                    }
                    return retbyte;
                }
                catch (Exception)
                {
                    return result;
                }
            }

    public static byte[] GzipCompress(byte[] data)
    {
    if (data == null || data.Length < 1)
    return data;
    try
    {
    using (MemoryStream stream = new MemoryStream())
    {
    using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
    {
    gZipStream.Write(data, 0, data.Length);
    gZipStream.Close();
    }
    return stream.ToArray();
    }
    }
    catch (Exception)
    {
    return data;
    }

    
    

    }

     
    byte[] retbyte = GzipCompress(retvalue);   //retvalue为datatable 

    return Json(retbyte);

    解压缩:

            /// <summary>
            /// 解压数据。
            /// </summary>
            /// <param name="ret">api中获取的结果,被压缩过的</param>
            public static DataTable DecompressByteToDatatble(string ret)
            {
                byte[] retbyte = JsonConvert.DeserializeObject<byte[]>(ret);
                //解压后得到的字节数组。
                byte[] decBuffer = DecompressByte(retbyte);
                DataTable retdt = null;
                if (decBuffer != null && decBuffer.Length > 0)
                {
                    using (MemoryStream memoryStream = new MemoryStream(decBuffer))
                    {
                        BinaryFormatter binaryFormatter = new BinaryFormatter();
                        retdt = (DataTable)binaryFormatter.Deserialize(memoryStream);
                    }
                }
                return retdt;
            }

    /// <summary>
    /// 解压数据。
    /// </summary>
    /// <param name="ret">api中获取的结果,被压缩过的</param>
    public static DataTable DecompressByteToDatatble(string ret)
    {
    byte[] retbyte = JsonConvert.DeserializeObject<byte[]>(ret);
    //解压后得到的字节数组。
    byte[] decBuffer = DecompressByte(retbyte);
    DataTable retdt = null;
    if (decBuffer != null && decBuffer.Length > 0)
    {
    using (MemoryStream memoryStream = new MemoryStream(decBuffer))
    {
    BinaryFormatter binaryFormatter = new BinaryFormatter();
    retdt = (DataTable)binaryFormatter.Deserialize(memoryStream);
    }
    }
    return retdt;
    }

     
     ds = DecompressByteToDatatble(ret);   //ret为取到的json
  • 相关阅读:
    【NOIP2007】守望者的逃离
    20200321(ABC)题解 by 马鸿儒 孙晨曦
    20200320(ABC)题解 by 王一帆
    20200319(ABC)题解 by 王一帆 梁延杰 丁智辰
    20200314(ABC)题解 by 董国梁 蒋丽君 章思航
    20200309(ABC)题解 by 梁延杰
    20200307(DEF)题解 by 孙晨曦
    20200306(ABC)题解 by 孙晨曦
    20200305(DEF)题解 by 孙晨曦
    20200303(ABC)题解 by 王锐,董国梁
  • 原文地址:https://www.cnblogs.com/tiancaige/p/14416834.html
Copyright © 2011-2022 走看看