C#实现通过Gzip来对数据进行压缩和解压

C#实现通过Gzip来对数据进行压缩和解压

internal static byte[] Compress(byte[] data)
{
    using (var compressedStream = new MemoryStream())
    {
        using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
        {
            zipStream.Write(data, 0, data.Length);
            return compressedStream.ToArray();
        }
    }
}

internal static byte[] Decompress(byte[] data)
{
    using (var compressedStream = new MemoryStream(data))
    {
        using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
        {
            using (var resultStream = new MemoryStream())
            {
                zipStream.CopyTo(resultStream);
                return resultStream.ToArray();
            }
        }
    }
}

 

posted @ 2016-09-26 17:48  FrankYou  阅读(5572)  评论(0编辑  收藏  举报