public static string Compress(string uncompressedString) { byte[] byteData = Encoding.GetEncoding("GB18030").GetBytes(uncompressedString); using (MemoryStream srcStream = new MemoryStream(byteData)) { using (var destStream = new MemoryStream()) { using (GZipStream compress = new GZipStream(destStream, CompressionMode.Compress)) { srcStream.CopyTo(compress); } return Convert.ToBase64String(destStream.ToArray()); } } }
public static string DeCompress(string comppressedString) { byte[] byteInput = Convert.FromBase64String(comppressedString); using (var srcStream = new MemoryStream(byteInput)) { using (var destStream = new MemoryStream()) { using (GZipStream decompress = new GZipStream(srcStream, CompressionMode.Decompress)) { decompress.CopyTo(destStream); var resultBytes = destStream.ToArray(); return Encoding.GetEncoding("GB18030").GetString(resultBytes, 0, resultBytes.Length); } } } }