+ UCompress: 增加数据帧压缩类

This commit is contained in:
Sunny 2023-07-17 23:05:41 +08:00
parent 48e2274861
commit 7578b70e1c
2 changed files with 151 additions and 1 deletions

146
SunnyUI/Common/UCompress.cs Normal file
View File

@ -0,0 +1,146 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2023 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UCompress.cs
* :
* : V3.4
* : 2023-07-17
*
* 2023-07-17: V3.4.0
******************************************************************************/
using System;
using System.Runtime.InteropServices;
namespace Sunny.UI
{
public enum DataFrameCompressType
{
[DisplayText("None")]
None = 0,
[DisplayText("FastLZ")]
FastLZ = 1,
[DisplayText("ZLib")]
ZLib = 2,
[DisplayText("GZip")]
GZip = 3,
[DisplayText("Deflate")]
Deflate = 4
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct DataFrame
{
public DateTime DateTime;
public byte[] Data;
public int Index;
public int CRC;
public DataFrame(DateTime dateTime, byte[] data, int index, int crc)
{
DateTime = dateTime;
Data = data;
Index = index;
CRC = crc;
}
public static DataFrame Empty()
{
#if NET40
return new DataFrame(DateTime.MinValue, new byte[0], -1, 0);
#else
return new DataFrame(DateTime.MinValue, Array.Empty<byte>(), -1, 0);
#endif
}
}
public static class DataFrameCompressHelper
{
public static byte[] Compress(DataFrame frame, DataFrameCompressType compressType, out int compressedLength, out int decompressLength)
{
byte[] compressed;
decompressLength = frame.Data.Length;
switch (compressType)
{
case DataFrameCompressType.None:
compressed = frame.Data;
break;
case DataFrameCompressType.FastLZ:
if (FastLZ.CheckFastLZDll())
{
compressed = FastLZ.Compress(frame.Data, 0, frame.Data.Length);
decompressLength = Math.Max(frame.Data.Length * 2, 66);
}
else
{
throw new NullReferenceException("FastLZx86.dll and FastLZx64.dll not exists.");
}
break;
case DataFrameCompressType.ZLib:
compressed = frame.Data.ZLibCompress();
break;
case DataFrameCompressType.GZip:
compressed = frame.Data.GZipCompress();
break;
case DataFrameCompressType.Deflate:
compressed = frame.Data.DeflateCompress();
break;
default:
compressed = frame.Data;
break;
}
compressedLength = compressed.Length;
return compressed;
}
public static byte[] Decompress(byte[] package, DataFrameCompressType compressType, int decompressLength)
{
byte[] data;
switch (compressType)
{
case DataFrameCompressType.None:
data = package;
break;
case DataFrameCompressType.FastLZ:
if (FastLZ.CheckFastLZDll())
{
data = FastLZ.Decompress(package, 0, package.Length, decompressLength);
}
else
{
throw new NullReferenceException("FastLZx86.dll and FastLZx64.dll not exists.");
}
break;
case DataFrameCompressType.ZLib:
data = package.ZLibDecompress();
break;
case DataFrameCompressType.GZip:
data = package.GZipDecompress();
break;
case DataFrameCompressType.Deflate:
data = package.DeflateDecompress();
break;
default:
data = package;
break;
}
return data;
}
}
}

View File

@ -312,7 +312,11 @@ namespace Sunny.UI
public static bool CheckFastLZDll()
{
if (FileExist) return true;
if (File.Exists(DirEx.CurrentDir() + "FastLZx86.dll") && File.Exists(DirEx.CurrentDir() + "FastLZx64.dll")) return true;
if (File.Exists(DirEx.CurrentDir() + "FastLZx86.dll") && File.Exists(DirEx.CurrentDir() + "FastLZx64.dll"))
{
FileExist = true;
return true;
}
try
{