GZip 压缩测试

来源:互联网 发布:数据透视表求和显示0 编辑:程序博客网 时间:2024/04/29 23:08

用.net 自带的 GZipStream 测试,压缩比率用默认值进行测试

对于3K以下的数据进行压缩,发现数据会越压缩越大,对于大于3K的数据进行压缩才会有效果。


而用第三主工具 ICSharpCode.SharpZipLib; 进行压缩,如果都是用默认的值的话,发现效率跟自带的差挺多的。一个数据级别


/// <summary>
        /// GZip 压缩
        /// </summary>
        /// <param name="inBytes"></param>
        /// <returns></returns>
        public static byte[] GZipCompress(byte[] inBytes)
        {


            MemoryStream outStream = new MemoryStream();


            using (MemoryStream intStream = new MemoryStream(inBytes))
            {


                using (GZipStream Compress =new GZipStream(outStream, CompressionMode.Compress))
                {


                    intStream.CopyTo(Compress);
                }


            }


            return outStream.ToArray();


        }




        /// <summary>
        /// GZip 解压
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        public static byte[] GZipDecompress(byte[] inStream)
        {


            byte[] result = null;


            MemoryStream compressedStream = new MemoryStream(inStream);


            using (MemoryStream outStream = new MemoryStream())
            {


                using (GZipStream Decompress = new GZipStream(compressedStream, CompressionMode.Decompress))
                {


                    Decompress.CopyTo(outStream);


                    result = outStream.ToArray();


                }


            }


            return result;


        }


        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        public static byte[] t1(byte[] bt)
        {
            MemoryStream outStream = new MemoryStream();


            using (MemoryStream intStream = new MemoryStream(bt))
            {


                using (ICSharpCode.SharpZipLib.GZip.GZipOutputStream Compress = new ICSharpCode.SharpZipLib.GZip.GZipOutputStream(outStream))
                {


                    intStream.CopyTo(Compress);
                }


            }


            return outStream.ToArray();
        }


        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        public static byte[] t2(byte[] bt)
        {
            byte[] result = null;
            MemoryStream compressedStream = new MemoryStream(bt);
            using (MemoryStream outStream = new MemoryStream())
            {


                using (ICSharpCode.SharpZipLib.GZip.GZipInputStream Decompress = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(compressedStream))
                {


                    Decompress.CopyTo(outStream);


                    result = outStream.ToArray();


                }


            }
            return result;
        }

原创粉丝点击