using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.GZip; /// /// GZIP相关工具集 /// public partial class Utils { /// /// GZIP压缩 /// /// 压缩字符串 /// 压缩后字符串 public static string GZipEncode(string content) { return Bytes2Str(GZipEncode(Str2Bytes(content))); } /// /// GZIP压缩 /// /// 压缩字节流 /// 压缩后的字节流 public static byte[] GZipEncode(byte[] bytes) { MemoryStream ms = new MemoryStream(); GZipOutputStream gz = new GZipOutputStream(ms); gz.Write(bytes, 0, bytes.Length); gz.Close(); byte[] ret = ms.ToArray(); ms.Close(); return ret; } /// /// GZIP解压缩 /// /// 已压缩字符串 /// 解压缩后字符串 public static string GZipDecode(string content) { return Bytes2Str(GZipDecode(Str2Bytes(content))); } /// /// GZIP解压缩 /// /// 已压缩字节流 /// 解压缩后字节流 public static byte[] GZipDecode(byte[] bytes) { MemoryStream des = new MemoryStream(); MemoryStream ms = new MemoryStream(bytes); GZipInputStream gz = new GZipInputStream(ms); int count = 0; int offset = 0; byte[] buf = new byte[1024 * 1024]; do { count = gz.Read(buf, 0, buf.Length); des.Write(buf, 0, count); offset += count; } while (count > 0); gz.Close(); byte[] ret = des.ToArray(); ms.Close(); return ret; } }