.NET C# 压缩工具类

1.工具类

using System;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;

namespace Infrastructure
{
    public class CompressionExtensions
    {
        #region 压缩

        #region CompressXmlDocument
        /// <summary>
        /// 压缩一个XML文档
        /// </summary>
        /// <param name="input">The XML document to compress.</param>
        /// <returns>Returns the compressed XML document.</returns>
        public static byte[] CompressXmlDocument(XmlDocument input)
        {
            return CompressString(input.InnerXml);
        }
        #endregion

        #region CompressDataSet
        /// <summary>
        /// 压缩DataSet
        /// </summary>
        /// <param name="input">The DataSet to compress.</param>
        /// <returns>Returns the compressed DataSet.</returns>
        public static byte[] CompressDataSet(DataSet input)
        {
            BinaryFormatter bf = new BinaryFormatter();

            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, input);
                return CompressBytes(ms.GetBuffer());
            }
        }
        #endregion

        #region CompressString
        /// <summary>
        /// 压缩字符串
        /// </summary>
        /// <param name="unCompressedString"></param>
        /// <returns></returns>
        public static byte[] CompressString(string unCompressedString)
        {
            byte[] bytData = System.Text.Encoding.Default.GetBytes(unCompressedString);
            return CompressBytes(bytData);
        }
        #endregion CompressString

        #region CompressBytes

        /// <summary>
        /// 压缩字节数组
        /// </summary>
        public static byte[] CompressBytes(byte[] data)
        {
            using (MemoryStream output = new MemoryStream())
            {
                GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
                gzip.Write(data, 0, data.Length);
                gzip.Close();
                data = null;
                return output.ToArray();
            }
        }
        #endregion

        #endregion 压缩

        #region 解压缩

        #region DecompressXmlDocument
        /// <summary>
        /// 解压缩用CompressXmlDocument生成的Byte数组
        /// </summary>
        /// <param name="input">The buffer that contains the compressed
        /// stream with the XML document.</param>
        /// <returns>Returns the decompressed XML document.</returns>
        public static XmlDocument DecompressXmlDocument(byte[] input)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(DecompressString(input));
            return doc;
        }

        #endregion

        #region DecompresString

        /// <summary>
        /// 解压缩用CompressString压缩生成的字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string DecompressString(byte[] data)
        {
            return System.Text.Encoding.Default.GetString(DecompressBytes(data));
        }

        /// <summary>
        /// 解压缩用CompressString压缩生成的字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string DecompressStringUTF8(byte[] data)
        {
            return System.Text.Encoding.UTF8.GetString(DecompressBytes(data));
        }

        #endregion

        #region DecompressDataSet

        /// <summary>
        /// 解压缩用CompressDataSet生成的Byte数组
        /// </summary>
        /// <param name="input">The buffer that contains the compressed
        /// stream with the DataSet.</param>
        /// <returns>Returns the decompressed DataSet.</returns>
        public static DataSet DecompressDataSet(byte[] input)
        {
            BinaryFormatter bf = new BinaryFormatter();

            byte[] buffer = DecompressBytes(input);
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                return (DataSet)bf.Deserialize(ms);
            }
        }

        #endregion

        #region DecompressBytes

        /// <summary>
        /// 解压字节数组
        /// </summary>
        /// <param name="data">The buffer that contains the compressed
        /// stream with the bytes.</param>
        /// <returns>Returns the decompressed bytes.</returns>
        public static byte[] DecompressBytes(byte[] data)
        {
            using (MemoryStream input = new MemoryStream(), output = new MemoryStream())
            {
                input.Write(data, 0, data.Length);
                input.Position = 0;
                GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);

                byte[] buff = new byte[4096];
                int read = -1;
                read = gzip.Read(buff, 0, buff.Length);
                while (read > 0)
                {
                    output.Write(buff, 0, read);
                    read = gzip.Read(buff, 0, buff.Length);
                }
                gzip.Close();
                data = null;
                return output.ToArray();
            }
        }

        /// <summary>
        /// 解压字节数组
        /// </summary>
        /// <param name="data">The buffer that contains the compressed
        /// stream with the bytes.</param>
        /// <returns>Returns the decompressed bytes.</returns>
        public static string DecompressBytes(string str)
        {
            //LogHelper.WriteLogInfo(typeof(CompressionEx).ToString(), "str:" + str);
            if (!string.IsNullOrEmpty(str))
            {
                //过滤特殊字符,并且补齐
                str = str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
                if (str.Length % 4 > 0)
                {
                    str = str.PadRight(str.Length + 4 - str.Length % 4, ‘=‘);
                }

                byte[] data = Convert.FromBase64String(str);
                //byte[] data = System.Text.Encoding.Default.GetBytes(str);
                return DecompressStringUTF8(data);
            }
            else
            {
                return "";
            }
        }

        #endregion

        #endregion 解压缩

        #region 其他辅助函数

        #region ToBase64String
        /// <summary>
        /// 转成可以在网上传输的Base64编码字符串
        /// </summary>
        public static string ToBase64String(byte[] data)
        {
            return Convert.ToBase64String(data);
        }
        #endregion

        #region FromBase64String
        /// <summary>
        /// 从Base64编码字符串转成字节数组
        /// </summary>
        public static byte[] FromBase64String(string str)
        {
            return Convert.FromBase64String(str);
        }
        #endregion

        #endregion 其他辅助函数
    }
}

2.调用方式

string str="xxxx";

byte[] bytes = CompressionExtensions.CompressString(str);//压缩字符串为字节数组

string temp="";

temp = CompressionExtensions.DecompressString(bytes);//对字节数组解压缩成字符串

 

.NET C# 压缩工具类

上一篇:Flask一种通用视图,增删改查RESTful API的设计


下一篇:关于windows系统服务的操作