• 压缩和解压方法


     public static class SlGZip
        {
            /// <summary>
            /// 压缩
            /// </summary>
            /// <param name="input">输入</param>
            /// <returns>结果</returns>
            public static string Compress(string input)
            {
                string result = string.Empty;
                if (!string.IsNullOrEmpty(input))
                {
                    byte[] source = Encoding.UTF8.GetBytes(input);
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (GZipStream gzs = new GZipStream(memoryStream, CompressionMode.Compress, true))
                        {
                            gzs.Write(source, 0, source.Length);
                        }

                        memoryStream.Position = 0;

                        byte[] target = new byte[memoryStream.Length];
                        memoryStream.Read(target, 0, target.Length);

                        byte[] finalBuffer = new byte[target.Length + 4];
                        Buffer.BlockCopy(target, 0, finalBuffer, 4, target.Length);
                        Buffer.BlockCopy(BitConverter.GetBytes(source.Length), 0, finalBuffer, 0, 4);

                        result = System.Convert.ToBase64String(finalBuffer);
                    }
                }

                return result;
            }

            /// <summary>
            /// 解压
            /// </summary>
            /// <param name="input">输入</param>
            /// <returns>结果</returns>
            public static string Decompress(string input)
            {
                string result = string.Empty;
                if (!string.IsNullOrEmpty(input))
                {
                    byte[] source = System.Convert.FromBase64String(input);
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        int length = BitConverter.ToInt32(source, 0);
                        memoryStream.Write(source, 4, source.Length - 4);
                        memoryStream.Position = 0;
                        byte[] decmpBytes = new byte[length];
                        using (GZipStream gzs = new GZipStream(memoryStream, CompressionMode.Decompress))
                        {
                            gzs.Read(decmpBytes, 0, length);
                        }

                        result = Encoding.UTF8.GetString(decmpBytes);
                    }
                }

                return result;
            }

            /// <summary>
            /// byte数组GZip压缩
            /// </summary>
            /// <param name="bytes"></param>
            /// <returns></returns>
            public static byte[] Compress(byte[] bytes)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    GZipStream Compress = new GZipStream(ms, CompressionMode.Compress);
                    Compress.Write(bytes, 0, bytes.Length);
                    Compress.Close();
                    return ms.ToArray();

                }
            }

            /// <summary>
            /// byte数组GZip解压缩
            /// </summary>
            /// <param name="bytes"></param>
            /// <returns></returns>
            public static byte[] Decompress(Byte[] bytes)
            {
                using (MemoryStream tempMs = new MemoryStream())
                {
                    using (MemoryStream ms = new MemoryStream(bytes))
                    {
                        GZipStream Decompress = new GZipStream(ms, CompressionMode.Decompress);
                        Decompress.CopyTo(tempMs);
                        Decompress.Close();
                        return tempMs.ToArray();
                    }
                }
            }
        }

  • 相关阅读:
    爱奇艺RN低代码引擎:千变万化、快速搭建的万花筒
    前端性能优化实战 https://mp.weixin.qq.com/s/amaR6GJFqhW3B9TspBLWJA
    递归 回文链表
    字节跳动自研高性能微服务框架 Kitex 的演进之旅 https://mp.weixin.qq.com/s/DhraH24FuojgW26M8KdODQ
    Cigarette smokers problem
    万字长文 | 十个模型,总结产品经理沟通方法论 https://mp.weixin.qq.com/s/cfAVn1dIk8eg1ushxd0sA
    贪心 Partition Labels
    rune
    层层剖析一次 HTTP POST 请求事故
    runtime: shrink map as elements are deleted 缩容 垃圾回收 GC 内存
  • 原文地址:https://www.cnblogs.com/sk2016/p/6088249.html
Copyright © 2020-2023  润新知