66 lines
2.1 KiB
C#
66 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Runtime.Serialization;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
|
|
namespace EAS.Security
|
|
{
|
|
#if SOA_NO
|
|
static class Compressor
|
|
#else
|
|
/// <summary>
|
|
/// 数据压缩。
|
|
/// </summary>
|
|
public static class Compressor
|
|
#endif
|
|
{
|
|
/// <summary>
|
|
/// 压缩数据。
|
|
/// </summary>
|
|
/// <param name="buffer">待压缩的数据。</param>
|
|
/// <returns>完成压缩的数据。</returns>
|
|
public static byte[] Compress(byte[] buffer)
|
|
{
|
|
MemoryStream ms = new MemoryStream();
|
|
using (DeflateStream zipStream = new DeflateStream(ms, CompressionMode.Compress, true))
|
|
{
|
|
zipStream.Write(buffer, 0, buffer.Length);
|
|
zipStream.Flush();
|
|
}
|
|
//zipStream.Close();
|
|
return ms.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 压缩数据。
|
|
/// </summary>
|
|
/// <param name="buffer">待压缩的数据。</param>
|
|
/// <returns>完成压缩的数据。</returns>
|
|
public static byte[] Decompress(byte[] buffer)
|
|
{
|
|
using (MemoryStream input = new MemoryStream())
|
|
{
|
|
input.Write(buffer, 0, buffer.Length);
|
|
input.Position = 0;
|
|
using (DeflateStream def = new DeflateStream(input, CompressionMode.Decompress))
|
|
{
|
|
using (MemoryStream output = new MemoryStream())
|
|
{
|
|
byte[] buff = new byte[64];
|
|
int read = -1;
|
|
read = def.Read(buff, 0, buff.Length);
|
|
while (read > 0)
|
|
{
|
|
output.Write(buff, 0, read);
|
|
read = def.Read(buff, 0, buff.Length);
|
|
}
|
|
return output.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |