首页 > 开发 > 综合 > 正文

Compression Helper Class using SharpZipLib

2024-07-21 02:15:54
字体:
来源:转载
供稿:网友


使用 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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表