namespace Microshaoft { using System; using System.Text; using System.IO; using System.IO.Compression; /// <summary> /// Crypto 的摘要说明。 /// </summary> public class CompressHelper { private static byte[] ReadStreamToBytes(Stream stream) { byte[] buffer = new byte[64 * 1024]; MemoryStream ms = new MemoryStream(); int r = 0; int l = 0; long position = -1; if (stream.CanSeek) { position = stream.Position; stream.Position = 0; } while (true) { r = stream.Read(buffer, 0, buffer.Length); if (r > 0) { l += r; ms.Write(buffer, 0, r); } else { break; } } byte[] bytes = new byte[l]; ms.Position = 0; ms.Read(bytes, 0, (int) l); ms.Close(); ms = null; if (position >= 0) { stream.Position = position; } return bytes; } public static byte[] GZipCompress(byte[] data) { MemoryStream ms = new MemoryStream(); GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true); stream.Write(data, 0, data.Length); stream.Close(); stream.Dispose(); stream = null; byte[] buffer = ReadStreamToBytes(ms); ms.Close(); ms.Dispose(); ms = null; return buffer; } public static byte[] GZipDecompress(byte[] data) { MemoryStream ms = new MemoryStream(data); GZipStream stream = new GZipStream(ms, CompressionMode.Decompress); byte[] buffer = ReadStreamToBytes(stream); ms.Close(); ms.Dispose(); ms = null; stream.Close(); stream.Dispose(); stream = null; return buffer; } public static byte[] DeflateCompress(byte[] data) { MemoryStream ms = new MemoryStream(); DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true); stream.Write(data, 0, data.Length); stream.Close(); stream.Dispose(); stream = null; byte[] buffer = ReadStreamToBytes(ms); ms.Close(); ms.Dispose(); ms = null; return buffer; } public static byte[] DeflateDecompress(byte[] data) { MemoryStream ms = new MemoryStream(data); DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress); byte[] buffer = ReadStreamToBytes(stream); ms.Close(); ms.Dispose(); ms = null; stream.Close(); stream.Dispose(); stream = null; return buffer; } } } namespace TestConsoleApplication { using System.Text; using System; using Microshaoft; /// <summary> /// Class1 的摘要说明。 /// </summary> public class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> //[STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // Console.WriteLine("Hello World"); Console.WriteLine(Environment.Version.ToString()); string s = "阿斯个贷哈根室电话个撒谎干大事个贷伽师将阿斯个贷哈根室电话个撒谎干大事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将事个贷伽师将"; byte[] buffer = Encoding.UTF8.GetBytes(s); byte[] bytes; bytes = CompressHelper.GZipCompress ( buffer ); Console.WriteLine("{0},GZip: {1}; {2}", buffer.Length, bytes.Length, s.Length); string ss = Encoding.UTF8.GetString ( ( CompressHelper.GZipDecompress ( bytes ) ) ); Console.WriteLine(ss); bytes = CompressHelper.DeflateCompress ( buffer ); Console.WriteLine("{0},Deflate: {1}; {2}", buffer.Length, bytes.Length, s.Length); //Console.WriteLine("{0},Deflate: {1}", buffer.Length, bytes.Length); ss = Encoding.UTF8.GetString ( ( CompressHelper.DeflateDecompress ( bytes ) ) ); Console.WriteLine(ss); } } }