• C# 压缩/解压字符串


    //调用压缩方法
    string compStr="abcdefg";
    var rawStr=GetStringByDataset(compStr);

           #region 压缩
            /// <summary>
            /// 根据DATASET压缩字符串
            /// </summary>
            /// <param name="ds"></param>
            /// <returns></returns>
            public static string GetStringByDataset(string ds)
            {
                return GZipCompressString(ds);
            }
    
            /// <summary>
            /// 将传入字符串以GZip算法压缩后,返回Base64编码字符
            /// </summary>
            /// <param name="rawString">需要压缩的字符串</param>
            /// <returns>压缩后的Base64编码的字符串</returns>
            public static string GZipCompressString(string rawString)
            {
                if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
                {
                    return "";
                }
                else
                {
                    byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
                    byte[] zippedData = Compress(rawData);
                    return (string)(Convert.ToBase64String(zippedData));
                }
            }
    
            /// <summary>
            /// GZip压缩
            /// </summary>
            /// <param name="rawData"></param>
            /// <returns></returns>
            static byte[] Compress(byte[] rawData)
            {
                MemoryStream ms = new MemoryStream();
                GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
                compressedzipStream.Write(rawData, 0, rawData.Length);
                compressedzipStream.Close();
                return ms.ToArray();
            }
            #endregion

    调用解压方法:str为压缩有的字符串

    var compStr=GetDatasetByString(Str);

        #region 解压
            /// <summary>
            /// 解压
            /// </summary>
            /// <param name="Value"></param>
            /// <returns></returns>
            public static string GetDatasetByString(string Value)
            {
                DataSet ds = new DataSet();
                string CC = GZipDecompressString(Value);
                return CC;
            }
            /// <summary>
            /// 将传入的二进制字符串资料以GZip算法解压缩
            /// </summary>
            /// <param name="zippedString">经GZip压缩后的二进制字符串</param>
            /// <returns>原始未压缩字符串</returns>
            public static string GZipDecompressString(string zippedString)
            {
                if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
                {
                    return "";
                }
                else
                {
                    byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
                    return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));
                }
            }
                    /// <summary>
            /// ZIP解压
            /// </summary>
            /// <param name="zippedData"></param>
            /// <returns></returns>
            public static byte[] Decompress(byte[] zippedData)
            {
                MemoryStream ms = new MemoryStream(zippedData);
                GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
                MemoryStream outBuffer = new MemoryStream();
                byte[] block = new byte[1024];
                while (true)
                {
                    int bytesRead = compressedzipStream.Read(block, 0, block.Length);
                    if (bytesRead <= 0)
                        break;
                    else
                        outBuffer.Write(block, 0, bytesRead);
                }
                compressedzipStream.Close();
                return outBuffer.ToArray();
            }
            #endregion
  • 相关阅读:
    当前毫秒时间戳
    生成随机指定长度的字符串
    symfony框架学习
    Git 学习一
    jmeter逻辑控制器
    jmeter执行顺序及作用域规则
    jmeter常用测试元件
    windows环境下jmeter生成测试报告
    jmeter参数化
    对网页进行截图(selenium)
  • 原文地址:https://www.cnblogs.com/xinbaba/p/7716459.html
Copyright © 2020-2023  润新知