namespace Microshaoft
{
using System;
using System.Text;
using System.IO;
using System.IO.Compression;
public class CompressHelper
{
public static byte[] ReadStreamToBytes(Stream stream)
{
byte[] buffer = new byte[64 * 1024];
int r = 0;
int l = 0;
long position = -1;
if (stream.CanSeek)
{
position = stream.Position;
stream.Position = 0;
}
MemoryStream ms = new MemoryStream();
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 Stream GZipCompress(Stream DATA)
{
byte[] buffer = ReadStreamToBytes(DATA);
MemoryStream ms = new MemoryStream();
GZipStream stream = new GZipStream(ms, CompressionMode.Compress, true);
stream.Write(buffer, 0, buffer.Length);
stream.Close();
stream.Dispose();
stream = null;
if (ms.CanSeek)
{
ms.Position = 0;
}
return ms;
}
public static Stream GZipDecompress(Stream data)
{
byte[] buffer = ReadStreamToBytes(data);
MemoryStream ms = new MemoryStream(buffer);
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
if (stream.CanSeek)
{
stream.Position = 0;
}
return stream;
}
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;
}
public static Stream DeflateCompress(Stream DATA)
{
byte[] buffer = ReadStreamToBytes(DATA);
MemoryStream ms = new MemoryStream();
DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true);
stream.Write(buffer, 0, buffer.Length);
stream.Close();
stream.Dispose();
stream = null;
if (ms.CanSeek)
{
ms.Position = 0;
}
return ms;
}
public static Stream DeflateDecompress(Stream data)
{
byte[] buffer = ReadStreamToBytes(data);
MemoryStream ms = new MemoryStream(buffer);
DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress);
if (stream.CanSeek)
{
stream.Position = 0;
}
return stream;
}
}
}
namespace TestConsoleApplication
{
using System.Text;
using System;
using System.IO;
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
);
//bytes = CompressHelper.ReadStreamToBytes(ms);
//string ss = Encoding.UTF8.GetString(bytes);
Stream ms = new MemoryStream(bytes);
//ms.Write(bytes, 0, bytes.Length);
ms.Position = 0;
ms = CompressHelper.GZipDecompress
(
ms
);
bytes = CompressHelper.ReadStreamToBytes(ms);
string ss = Encoding.UTF8.GetString(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);
}
}
}