欢迎光临
我们一直在努力

Compression Helper Class using SharpZipLib-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

使用 sharpziplib 进行压缩的辅助类,简化压缩字节数组和字符串的操作。

using system;

using system.text;

using system.io;

using icsharpcode.sharpziplib.bzip2;

using icsharpcode.sharpziplib.gzip;

using icsharpcode.sharpziplib.zip;

 

namespace compression

{

    /// <summary>

    /// 压缩方式。

    /// </summary>

    public enum compressiontype

    {

        gzip,

        bzip2,

        zip

    }

 

    /// <summary>

    /// 使用 sharpziplib 进行压缩的辅助类,简化压缩字节数组和字符串的操作。

    /// </summary>

    public class compressionhelper

    {

        /// <summary>

        /// 压缩供应者,默认为 gzip。

        /// </summary>

        public static compressiontype compressionprovider = compressiontype.gzip;

 

        #region public methods

 

        /// <summary>

        /// 从原始字节数组生成已压缩的字节数组。

        /// </summary>

        /// <param name=”bytestocompress”>原始字节数组。</param>

        /// <returns>返回已压缩的字节数组</returns>

        public static byte[] compress( byte[] bytestocompress )

        {

            memorystream ms = new memorystream();

            stream s = outputstream( ms );

            s.write( bytestocompress, 0, bytestocompress.length );

            s.close();

            return ms.toarray();

        }

 

        /// <summary>

        /// 从原始字符串生成已压缩的字符串。

        /// </summary>

        /// <param name=”stringtocompress”>原始字符串。</param>

        /// <returns>返回已压缩的字符串。</returns>

        public static string compress( string stringtocompress )

        {

            byte[] compresseddata = compresstobyte( stringtocompress );

            string strout = convert.tobase64string( compresseddata );

            return strout;

        }

 

        /// <summary>

        /// 从原始字符串生成已压缩的字节数组。

        /// </summary>

        /// <param name=”stringtocompress”>原始字符串。</param>

        /// <returns>返回已压缩的字节数组。</returns>

        public static byte[] compresstobyte( string stringtocompress )

        {

            byte[] bytdata = encoding.unicode.getbytes( stringtocompress );

            return compress( bytdata );

        }

 

        /// <summary>

        /// 从已压缩的字符串生成原始字符串。

        /// </summary>

        /// <param name=”stringtodecompress”>已压缩的字符串。</param>

        /// <returns>返回原始字符串。</returns>

        public string decompress( string stringtodecompress )

        {

            string outstring = string.empty;

            if ( stringtodecompress == null )

            {

                throw new argumentnullexception(“stringtodecompress”, “you tried to use an empty string”);

            }

 

            try

            {

                byte[] inarr = convert.frombase64string(stringtodecompress.trim());

                outstring = encoding.unicode.getstring(decompress(inarr));

            }

            catch (nullreferenceexception  nex)

            {

                return nex.message;

            }

 

            return outstring;

        }

 

        /// <summary>

        /// 从已压缩的字节数组生成原始字节数组。

        /// </summary>

        /// <param name=”bytestodecompress”>已压缩的字节数组。</param>

        /// <returns>返回原始字节数组。</returns>

        public static byte[] decompress( byte[] bytestodecompress )

        {

            byte[] writedata = new byte[4096];

            stream s2 = inputstream( new memorystream( bytestodecompress ) );

            memorystream outstream = new memorystream();

 

            while (true)

            {

                int size = s2.read( writedata, 0, writedata.length );

                if ( size > 0 )

                {

                    outstream.write( writedata, 0, size );

                }

                else

                {

                    break;

                }

            }

            s2.close();

            byte[] outarr = outstream.toarray();

            outstream.close();

            return outarr;

        }

 

        #endregion

 

        #region private methods

 

        /// <summary>

        /// 从给定的流生成压缩输出流。

        /// </summary>

        /// <param name=”inputstream”>原始流。</param>

        /// <returns>返回压缩输出流。</returns>

        private static stream outputstream( stream inputstream )

        {

            switch ( compressionprovider )

            {

                case compressiontype.bzip2:

                    return new bzip2outputstream( inputstream );

 

                case compressiontype.gzip:

                    return new gzipoutputstream( inputstream );

 

                case compressiontype.zip:

                    return new zipoutputstream( inputstream );

 

                default:

                    return new gzipoutputstream( inputstream );

            }

        }

 

        /// <summary>

        /// 从给定的流生成压缩输入流。

        /// </summary>

        /// <param name=”inputstream”>原始流。</param>

        /// <returns>返回压缩输入流。</returns>

        private static stream inputstream( stream inputstream )

        {

            switch (compressionprovider)

            {

                case compressiontype.bzip2:

                    return new bzip2inputstream(inputstream);

 

                case compressiontype.gzip:

                    return new gzipinputstream(inputstream);

 

                case compressiontype.zip:

                    return new zipinputstream(inputstream);

 

                default:

                    return new gzipinputstream(inputstream);

            }

        }

 

        #endregion

    }

}

来源:http://www.mostlylucid.co.uk/archive/2004/04/06/958.aspx

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Compression Helper Class using SharpZipLib-.NET教程,Asp.Net开发
分享到: 更多 (0)