zoukankan      html  css  js  c++  java
  • C# GZip对字符串压缩和解压

         /// <summary>
            /// 压缩方法
            /// </summary>
            public static string CompressString(string str)
            {
                string compressString = "";
                byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);
                byte[] compressAfterByte = Compress(compressBeforeByte);
                compressString = Convert.ToBase64String(compressAfterByte);
                return compressString;
            }
    
            public static byte[] Compress(byte[] data)
            {
                try
                {
                    MemoryStream ms = new MemoryStream();
                    GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
                    zip.Write(data, 0, data.Length);
                    zip.Close();
                    byte[] buffer = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(buffer, 0, buffer.Length);
                    ms.Close();
                    return buffer;
    
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
    
            /// <summary>
            /// 字符串解压缩
            /// </summary>
            public static string DecompressString(string str)
            {
                string compressString = "";
                byte[] compressBeforeByte = Convert.FromBase64String(str);
                byte[] compressAfterByte = Decompress(compressBeforeByte);
                compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);
                return compressString;
            }
           
            public static byte[] Decompress(byte[] data)
            {
                try
                {
                    MemoryStream ms = new MemoryStream(data);
                    GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
                    MemoryStream msreader = new MemoryStream();
                    byte[] buffer = new byte[0x1000];
                    while (true)
                    {
                        int reader = zip.Read(buffer, 0, buffer.Length);
                        if (reader <= 0)
                        {
                            break;
                        }
                        msreader.Write(buffer, 0, reader);
                    }
                    zip.Close();
                    ms.Close();
                    msreader.Position = 0;
                    buffer = msreader.ToArray();
                    msreader.Close();
                    return buffer;
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
  • 相关阅读:
    关于Dubbo和Spring异步注解@Async的冲突
    查看和解除Linux系统对用户使用资源的限制
    Spring 邮件发送
    分布式一致性哈希算法
    Java实现三大简单排序算法
    Java二维码生成与解码
    第三方支付之微信支付(扫码支付)
    第三方支付之支付宝(电脑网站支付)
    集成第三方开放平台
    Spring动态数据源实现读写分离
  • 原文地址:https://www.cnblogs.com/ericli-ericli/p/5737562.html
Copyright © 2011-2022 走看看