C#实现通过Gzip来对数据进行压缩和解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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(); } } } } |