Fork me on GitHub

Telerik字符压缩算法

using Telerik.Windows.Zip;

        /// <summary> 
        /// 利用telerik的zip库压缩字符串 
        /// </summary> 
        /// <param name="str"></param> 
        /// <returns></returns> 
        public static string CompressString(string str)
        {
            string result = string.Empty;
            try
            {
                var memoryStream = new MemoryStream();
                var zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Default);
                var writer = new StreamWriter(zipOutputStream);
                writer.Write(str);
                writer.Flush();
                result = Convert.ToBase64String(memoryStream.ToArray());
            }
            catch { }
            return result;
        }

        /// <summary> 
        /// 利用telerik的zip库解压字符串 
        /// </summary> 
        /// <param name="compressedBase64String"></param> 
        /// <returns></returns> 
        public static string UnCompressString(string compressedBase64String)
        {
            string result = string.Empty;
            try
            {
                var memoryStream = new MemoryStream(Convert.FromBase64String(compressedBase64String));
                var input = new ZipInputStream(memoryStream);
                var reader = new StreamReader(input);
                result = reader.ReadToEnd();
            }
            catch { }
            return result;
        }

 

posted @ 2012-11-12 17:08  Nick.Chung  阅读(181)  评论(0编辑  收藏  举报