直接给码:
using System; using System.IO; using Telerik.Windows.Zip; namespace JIMMY { public static class ZipHelper { /// <summary> /// 利用telerik的zip库压缩字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string CompressString(string str) { string result = string.Empty; try { MemoryStream memoryStream = new MemoryStream(); ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Default); StreamWriter 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 { MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(compressedBase64String)); ZipInputStream input = new ZipInputStream(memoryStream); StreamReader reader = new StreamReader(input); result = reader.ReadToEnd(); } catch { } return result; } } }
杨过友情提示:该方法只适用于较大字符串,如果字符串很短,压缩后的字符串长度有可能更大。