NoSugarNet/NoSugarNet.Adapter/DataHelper/CompressAdapter.cs
2024-06-25 14:50:22 +08:00

200 lines
5.6 KiB
C#

using System.IO;
using System.IO.Compression;
namespace NoSugarNet.Adapter.DataHelper
{
public enum E_CompressAdapter
{
//不压缩
None = 0,
//GIPZ
GZIP_Plan1 = 1,
//Deflate
DEFLATE = 2,
//Brotli
BROTLI = 3,
//LZ4
LZ4 = 4,
}
/// <summary>
/// 压缩适配器
/// </summary>
public class CompressAdapter
{
IDataCompress mIDataCompress;
public CompressAdapter(E_CompressAdapter type)
{
switch (type)
{
//不压缩
case E_CompressAdapter.None:
mIDataCompress = new NoCompress();
break;
//GZIP Plan1
case E_CompressAdapter.GZIP_Plan1:
mIDataCompress = new GZipCompress();
break;
//Deflate
case E_CompressAdapter.DEFLATE:
mIDataCompress = new DeflateCompress();
break;
//Brotli
case E_CompressAdapter.BROTLI:
mIDataCompress = new BrotliCompress();
break;
//LZ4
case E_CompressAdapter.LZ4:
mIDataCompress = new LZ4Compress();
break;
//TODO 其他压缩对比
//……
default:
mIDataCompress = new NoCompress();
break;
}
}
public byte[] Compress(byte[] data)
{
return mIDataCompress.Compress(data);
}
public byte[] Decompress(byte[] data)
{
return mIDataCompress.Decompress(data);
}
}
public interface IDataCompress
{
byte[] Compress(byte[] data);
byte[] Decompress(byte[] data);
}
public class NoCompress : IDataCompress
{
public byte[] Compress(byte[] data)
{
return data;
}
public byte[] Decompress(byte[] data)
{
return data;
}
}
//Install-Package BrotliSharpLib
//using BrotliSharpLib;
public class BrotliCompress : IDataCompress
{
public byte[] Compress(byte[] data)
{
return Brotli.Encode(data, 0, data.Length);
}
public byte[] Decompress(byte[] data)
{
return Brotli.Decode(data, 0, data.Length);
}
}
//Install-Package lz4net
//using LZ4;
public class LZ4Compress : IDataCompress
{
public byte[] Compress(byte[] data)
{
byte[] compressedData;
using (var memoryStream = new MemoryStream())
{
using (var lz4Stream = LZ4Stream.Encode(memoryStream, LZ4StreamMode.Compress))
{
lz4Stream.Write(data, 0, data.Length);
}
compressedData = memoryStream.ToArray();
}
return compressedData;
}
public byte[] Decompress(byte[] data)
{
byte[] decompressedData;
using (var memoryStream = new MemoryStream(data))
using (var lz4Stream = LZ4Stream.Decode(memoryStream, LZ4StreamMode.Decompress))
using (var resultStream = new MemoryStream())
{
lz4Stream.CopyTo(resultStream);
decompressedData = resultStream.ToArray();
}
return decompressedData;
}
}
public class DeflateCompress : IDataCompress
{
public byte[] Compress(byte[] data)
{
using (var compressedStream = new MemoryStream())
{
using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Compress, true))
{
deflateStream.Write(data, 0, data.Length);
}
return compressedStream.ToArray();
}
}
public byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
deflateStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
}
public class GZipCompress : IDataCompress
{
//public byte[] Compress(byte[] data)
//{
// using (var compressedStream = new MemoryStream())
// using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
// {
// zipStream.Write(data, 0, data.Length);
// zipStream.Close();
// return compressedStream.ToArray();
// }
//}
public byte[] Compress(byte[] data)
{
using (var compressedStream = new MemoryStream())
{
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress, true))
{
zipStream.Write(data, 0, data.Length);
}
return compressedStream.ToArray();
}
}
public byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
}
}