C#實現通過Gzip來對資料進行壓縮和解壓

FrankYou發表於2016-09-26

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();
            }
        }
    }
}

 

相關文章