Byte流的压缩小技巧

使用Lz4:

public class Lz4Tool { public static byte[] CompressBytes(byte[] bytes) { return LZ4Codec.Wrap(bytes); } public static byte[] Decompress(byte[] bytes) { return LZ4Codec.Unwrap(bytes); } }

GZip:

public static class ZipTool { public static byte[] CompressBytes(byte[] bytes) { using (MemoryStream compressStream = new MemoryStream()) { using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress)) zipStream.Write(bytes, 0, bytes.Length); return compressStream.ToArray(); } } public static byte[] Decompress(byte[] bytes) { using (var compressStream = new MemoryStream(bytes)) { using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress)) { using (var resultStream = new MemoryStream()) { zipStream.CopyTo(resultStream); return resultStream.ToArray(); } } } } }

posted @ 2022-01-27 14:27  supersdar  阅读(302)  评论(0编辑  收藏  举报