.Net常用技巧_解压缩byte数组

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数组,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。