• 压缩解压缩传输的数据


    压缩:

      /// <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
  • 相关阅读:
    目标检测网络CenterNet详解(四)
    样本不均衡问题
    目标检测网络Faster RCNN详解(一)
    SpringCloud学习总结(八)——服务调用Feign
    OpenFeign(2020-10-13)
    Feign真正正确的使用方法
    微服务实战SpringCloud之Feign简介及使用
    spring cloud gateway网关和负载均衡框架ribbon实战
    Studio 3T 破解
    JVM 垃圾回收?全面详细安排!
  • 原文地址:https://www.cnblogs.com/tiancaige/p/14416834.html
Copyright © 2020-2023  润新知