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
  • 相关阅读:
    记一次CTF比赛过程与解题思路MISC部分
    使用requests爬虫遇到的一个奇葩的问题:UnicodeEncodeError: 'latin1' codec can't encode character
    纯前端实现词云展示+附微博热搜词云Demo代码
    亚马逊精细化选品服务
    乔布斯访谈笔记
    使用腾讯云轻量级服务器
    centos 设置阿里的yum源
    云未来、新可能 绿色、无处不在、可信的计算
    OpenKruise v1.0:云原生应用自动化达到新的高峰
    服务发现与配置管理高可用最佳实践
  • 原文地址:https://www.cnblogs.com/tiancaige/p/14416834.html
Copyright © 2011-2022 走看看