关于System.IO.Compression.GZipStream的使用
//添加引用
using System.IO.Compression;
//压缩数组
public unsafe static byte[] Compress(byte[] data)
{
MemoryStream ms = new MemoryStream();
Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
zipStream.Write(data, 0, data.Length);
zipStream.Close();
byte[] newByteArray = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(newByteArray, 0, newByteArray.Length);
//zipStream.Write(buffer, 0, buffer.Length);
//zipStream.Read(newByteArray, 0, (int)ms.Length);
return newByteArray;
}
//解压缩数组
public unsafe static byte[] DeCompress(byte[] data)
{
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Seek(0, SeekOrigin.Begin);
Stream zipStream = new GZipStream(ms, CompressionMode.Decompress,false);
byte[] newByteArray = RetrieveBytesFromStream(zipStream, 1);
return newByteArray;
}
public unsafe static byte[] RetrieveBytesFromStream(Stream stream, int bytesblock)
{
//---从一个流对象中检索字节---
List<byte> lst = new List<byte>();
byte[] data=new byte[2];
int totalCount = 0;
try
{
while (true)
{
//'---逐渐地增加数据字节数组-的大小--
int bytesRead = stream.Read(data, totalCount, bytesblock);
if (bytesRead == 0)
{
break;
}
lst.Add(data[data.Length - 1]);
totalCount += bytesRead;
data = new byte[totalCount+1];
}
//'---确保字节数组正确包含提取的字节数---
byte[] data2 = new byte[totalCount];
lst.CopyTo(data2, 0);
return data2;
}
catch (Exception ex)
{
//MsgBox(ex.ToString)
return new byte[0];
}
}
private void button1_Click(object sender, EventArgs e)
{
int len = 10*1024;
byte[] data = new byte[len];
Random rnd = new Random(255);
for (int i = 0; i < len; i++)
{
data[i] = (byte)(int)(i/45);
}
int t1 = System.Environment.TickCount;
byte[] data2 = Compress(data);
//byte[] data3 = DeCompress(data2);
int t2=System.Environment.TickCount;
MessageBox.Show(string.Format("耗时 {0} 耗秒.压缩比{1}/{2}={3}", t2 - t1, data2.Length, data.Length, data2.Length*1.0/data.Length));
}
using System.IO.Compression;
//压缩数组
public unsafe static byte[] Compress(byte[] data)
{
MemoryStream ms = new MemoryStream();
Stream zipStream = new GZipStream(ms, CompressionMode.Compress, true);
zipStream.Write(data, 0, data.Length);
zipStream.Close();
byte[] newByteArray = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(newByteArray, 0, newByteArray.Length);
//zipStream.Write(buffer, 0, buffer.Length);
//zipStream.Read(newByteArray, 0, (int)ms.Length);
return newByteArray;
}
//解压缩数组
public unsafe static byte[] DeCompress(byte[] data)
{
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, data.Length);
ms.Seek(0, SeekOrigin.Begin);
Stream zipStream = new GZipStream(ms, CompressionMode.Decompress,false);
byte[] newByteArray = RetrieveBytesFromStream(zipStream, 1);
return newByteArray;
}
public unsafe static byte[] RetrieveBytesFromStream(Stream stream, int bytesblock)
{
//---从一个流对象中检索字节---
List<byte> lst = new List<byte>();
byte[] data=new byte[2];
int totalCount = 0;
try
{
while (true)
{
//'---逐渐地增加数据字节数组-的大小--
int bytesRead = stream.Read(data, totalCount, bytesblock);
if (bytesRead == 0)
{
break;
}
lst.Add(data[data.Length - 1]);
totalCount += bytesRead;
data = new byte[totalCount+1];
}
//'---确保字节数组正确包含提取的字节数---
byte[] data2 = new byte[totalCount];
lst.CopyTo(data2, 0);
return data2;
}
catch (Exception ex)
{
//MsgBox(ex.ToString)
return new byte[0];
}
}
private void button1_Click(object sender, EventArgs e)
{
int len = 10*1024;
byte[] data = new byte[len];
Random rnd = new Random(255);
for (int i = 0; i < len; i++)
{
data[i] = (byte)(int)(i/45);
}
int t1 = System.Environment.TickCount;
byte[] data2 = Compress(data);
//byte[] data3 = DeCompress(data2);
int t2=System.Environment.TickCount;
MessageBox.Show(string.Format("耗时 {0} 耗秒.压缩比{1}/{2}={3}", t2 - t1, data2.Length, data.Length, data2.Length*1.0/data.Length));
}