天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 lines
3.5 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Common.IO.Compression
{
public class DeflateCompression
{
/// <summary>
/// 压缩字节流
/// </summary>
/// <param name="baseStream">原字节流</param>
/// <returns>返回压缩后的字节流</returns>
public static byte[] Compress(byte[] baseStream)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (System.IO.Compression.DeflateStream gs = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress))
{
gs.Write(baseStream, 0, baseStream.Length);
gs.Flush();
}
byte[] rs = ms.ToArray();
ms.Close();
return rs;
}
/// <summary>
/// 解压缩字节流
/// </summary>
/// <param name="compressedStream">经过压缩的字节流</param>
/// <returns>返回解压缩之后的字节流</returns>
public static byte[] Decompress(byte[] compressedStream)
{
byte[] buff = new byte[1024];
using (System.IO.MemoryStream comp = new System.IO.MemoryStream(compressedStream))
using (System.IO.Compression.DeflateStream gs = new System.IO.Compression.DeflateStream(comp, System.IO.Compression.CompressionMode.Decompress))
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
comp.Position = 0;
int rs = 0;
rs = gs.Read(buff, 0, buff.Length);
while (rs > 0)
{
ms.Write(buff, 0, rs);
rs = gs.Read(buff, 0, buff.Length);
}
buff = ms.ToArray();
}
return buff;
}
/// <summary>
/// 压缩字节流
/// </summary>
/// <param name="baseStream">原字节流</param>
/// <returns>返回压缩后的字节流</returns>
public static System.IO.MemoryStream Compress(System.IO.MemoryStream baseStream)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (System.IO.Compression.DeflateStream gs = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress))
{
gs.Write(baseStream.ToArray(), 0, (int)baseStream.Length);
gs.Flush();
}
return ms;
}
/// <summary>
/// 解压缩字节流
/// </summary>
/// <param name="compressedStream">经过压缩的字节流</param>
/// <returns>返回解压缩之后的字节流</returns>
public static System.IO.MemoryStream Decompress(System.IO.MemoryStream compressedStream)
{
byte[] buff = new byte[1024];
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (System.IO.Compression.DeflateStream gs = new System.IO.Compression.DeflateStream(compressedStream, System.IO.Compression.CompressionMode.Decompress))
{
compressedStream.Position = 0;
int rs = 0;
rs = gs.Read(buff, 0, buff.Length);
while (rs > 0)
{
ms.Write(buff, 0, rs);
rs = gs.Read(buff, 0, buff.Length);
}
}
return ms;
}
}
}