现在的位置: 首页 > 综合 > 正文

微软Gzip压缩算法

2013年02月06日 ⁄ 综合 ⁄ 共 821字 ⁄ 字号 评论关闭
public class GZipHelper
    {
        /// <summary>
        /// Gzip解压
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZipDecompress(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress))
                {
                    byte[] bytes = new byte[40960]; int n;
                    while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        stream.Write(bytes, 0, n);
                    }
                    gZipStream.Close();
                }
                return stream.ToArray();
            }
        }
        /// <summary>
        /// Gzip压缩
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZipCompress(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
                {
                    gZipStream.Write(data, 0, data.Length); gZipStream.Close();
                }
                return stream.ToArray();
            }
        }
    }

需要下载一个类库http://u.download.csdn.net/upload/success

抱歉!评论已关闭.