这几天做了个TCP通讯的东西,由于需要发送的数据太大,所以想办法将发送的时候给他压缩,然后再发送压缩过的数据,接受方也可以减轻接受的时间和分配过大的内存空间造成的缓慢运行问题.我就将代码放下做个记号.
public class Compress
{
/// <summary>
/// 压缩大的byte数组
/// </summary>
/// <param name="uncompressedByte">没有压缩的byte数组</param>
/// <returns>压缩后的byte数组</returns>
public static byte[] Compression(byte[] uncompressedByte)
{
MemoryStream ms = new MemoryStream();
using (Stream s = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms))
{
s.Write(uncompressedByte, 0, uncompressedByte.Length);
s.Close();
}
byte[] compressedData = (byte[])ms.ToArray();
return compressedData;
}
/// <summary>
/// 解压压缩的byte数组
/// </summary>
/// <param name="compressedByte">压缩的byte数组</param>
/// <param name="bufferLength">未压缩前的大小</param>
/// <returns>未压缩前的状态</returns>
public static byte[] DeCompression(byte[] compressedByte, int bufferLength)
{
byte[] bufferByte = new byte[bufferLength];
int totalLength = 0;
byte[] writeData = new byte[4096];
using (Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(compressedByte)))
{
while (true)
{
int size = s2.Read(writeData, 0, writeData.Length);
if (size > 0)
{
Buffer.BlockCopy(writeData, 0, bufferByte, totalLength, size);
totalLength += size;
}
else
{
break;
}
}
s2.Close();
}
return bufferByte;
}
/// <summary>
/// 解压
/// </summary>
/// <param name="compressedByte">需要解压的数组</param>
/// <returns></returns>
public static byte[] DeCompression(byte[] compressedByte)
{
MemoryStream ms = new MemoryStream();
byte[] writeData = new byte[4096];
using (Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(compressedByte)))
{
while (true)
{
int size = s2.Read(writeData, 0, writeData.Length);
if (size > 0)
{
ms.Write(writeData, 0, size);
}
else
{
break;
}
}
s2.Close();
}
return ms.ToArray();
}
}
使用他的时候我们需要添加第三方给我们提供的ICSharpCode.SharpZipLib.dll然后我们才可以使用,对字符的压缩比是很高的,我观察了下基本上可以压缩成100:1这样的效果.以后在遇见类似的情况可以参考下.
{
/// <summary>
/// 压缩大的byte数组
/// </summary>
/// <param name="uncompressedByte">没有压缩的byte数组</param>
/// <returns>压缩后的byte数组</returns>
public static byte[] Compression(byte[] uncompressedByte)
{
MemoryStream ms = new MemoryStream();
using (Stream s = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms))
{
s.Write(uncompressedByte, 0, uncompressedByte.Length);
s.Close();
}
byte[] compressedData = (byte[])ms.ToArray();
return compressedData;
}
/// <summary>
/// 解压压缩的byte数组
/// </summary>
/// <param name="compressedByte">压缩的byte数组</param>
/// <param name="bufferLength">未压缩前的大小</param>
/// <returns>未压缩前的状态</returns>
public static byte[] DeCompression(byte[] compressedByte, int bufferLength)
{
byte[] bufferByte = new byte[bufferLength];
int totalLength = 0;
byte[] writeData = new byte[4096];
using (Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(compressedByte)))
{
while (true)
{
int size = s2.Read(writeData, 0, writeData.Length);
if (size > 0)
{
Buffer.BlockCopy(writeData, 0, bufferByte, totalLength, size);
totalLength += size;
}
else
{
break;
}
}
s2.Close();
}
return bufferByte;
}
/// <summary>
/// 解压
/// </summary>
/// <param name="compressedByte">需要解压的数组</param>
/// <returns></returns>
public static byte[] DeCompression(byte[] compressedByte)
{
MemoryStream ms = new MemoryStream();
byte[] writeData = new byte[4096];
using (Stream s2 = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new MemoryStream(compressedByte)))
{
while (true)
{
int size = s2.Read(writeData, 0, writeData.Length);
if (size > 0)
{
ms.Write(writeData, 0, size);
}
else
{
break;
}
}
s2.Close();
}
return ms.ToArray();
}
}