码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-16 23:17:08      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   re   

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

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

标签:style   blog   color   os   io   re   

原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!