数据流压缩代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace CommonUtility.CompressionUtility
{
    public class CompressionUtility
    {
        /// <summary>
        /// 读取数据流中的字节
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] ReadStreamToBytes(Stream stream)
        {
            byte[] buffer = new byte[64 * 1024];
            MemoryStream ms = new MemoryStream();
            int r = 0;
            int l = 0;
            long position = -1;
            if (stream.CanSeek)
            {
                position = stream.Position;
                stream.Position = 0;
            }
            while (true)
            {
                r = stream.Read(buffer, 0, buffer.Length);
                if (r > 0)
                {
                    l += r;
                    ms.Write(buffer, 0, r);
                }
                else
                {
                    break;
                }
            }
            byte[] bytes = new byte[l];
            ms.Position = 0;
            ms.Read(bytes, 0, (int)l);
            ms.Close();
            ms = null;
            if (position >= 0)
            {
                stream.Position = position;
            }
            return bytes;
        }
        /// <summary>
        /// GZip算法压缩数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZipCompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
            stream.Write(data, 0, data.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            byte[] buffer = ReadStreamToBytes(ms);
            ms.Close();
            ms.Dispose();
            ms = null;
            return buffer;
        }
        /// <summary>
        /// GZip算法解压数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZipDecompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
            byte[] buffer = ReadStreamToBytes(stream);
            ms.Close();
            ms.Dispose();
            ms = null;
            stream.Close();
            stream.Dispose();
            stream = null;
            return buffer;
        }
        /// <summary>
        /// Deflate算法压缩数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DeflateCompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true);
            stream.Write(data, 0, data.Length);
            stream.Close();
            stream.Dispose();
            stream = null;
            byte[] buffer = ReadStreamToBytes(ms);
            ms.Close();
            ms.Dispose();
            ms = null;
            return buffer;
        }
        /// <summary>
        /// Deflate算法解压数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DeflateDecompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress);
            byte[] buffer = ReadStreamToBytes(stream);
            ms.Close();
            ms.Dispose();
            ms = null;
            stream.Close();
            stream.Dispose();
            stream = null;
            return buffer;
        }
    }
}