1,需要引用命名空间
using System.IO.Compression;
2, //压缩byte数组方法
private byte[] ComPress(byte[] data) { try { MemoryStream ms = new MemoryStream(); Stream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Compress, true); zipStream.Write(data, 0, data.Length); zipStream.Close(); ms.Seek(0, SeekOrigin.Begin); byte[] buffer = ms.ToArray(); ms.Close(); return buffer; } catch { return null; } }
3, //解压缩byte数组方法
private byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); MemoryStream msTmp = new MemoryStream(); GZipStream zipStream = null; zipStream = new GZipStream(ms, CompressionMode.Decompress, true); byte[] buff = new byte[512]; while (true) { int size = zipStream.Read(buff, 0, 512); msTmp.Write(buff, 0, size); if (size < 1) break; } zipStream.Close(); ms.Close(); byte[] bt = msTmp.ToArray(); msTmp.Close(); return bt; } catch { return null; } }
.Net常用技巧_解压缩byte数组,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811456.html