C# Gzip压缩byte[](非重写算法实现)

C#通过Gzip压缩byte[]

直接上代码

//压缩
public static byte[] GZipBytes( byte[] data)
{
	using (var ms=new MemoryStream())
	{
		using (var gzipstream=new  GZipStream(ms,CompressionMode.Compress))
 		{
 			gzipstream.Write(data,0,data.Length);
			return ms.ToArray();
		}
	}
}
//解压
 public static byte[] UGZipBytes(byte[] data)
        {
            using (var ms=new MemoryStream(data))
            {
                using (var gzs=new  GZipStream(ms,CompressionMode.Decompress))
                {
                    using (var outBuf=new MemoryStream())
                    {
                        byte[] tmp=new byte[1024];
                        int leng = 0;
                        while ((leng=gzs.Read(tmp,0,tmp.Length))>0)
                        {
                            outBuf.Write(tmp,0,leng);
                        }

                        return outBuf.ToArray();
                    }
                }
            }
        }

你可能感兴趣的:(C# Gzip压缩byte[](非重写算法实现))