* FastLZ: 增加了扩展的自定义压缩解压方法

This commit is contained in:
Sunny 2022-07-07 18:06:42 +08:00
parent 5e09012ad7
commit 1a9bb4f0da
4 changed files with 128 additions and 16 deletions

Binary file not shown.

Binary file not shown.

View File

@ -25,6 +25,8 @@
* compressed form.
*
* 2022-03-31: V3.1.2
* 2022-07-07: V3.2.1
* 2022-07-07: V3.2.1 FastLZx86.dllFastLZx64.dll资源文件
******************************************************************************
*
* int fastlz_compress_level(int level, const void* input, int length, void* output);
@ -53,11 +55,23 @@
******************************************************************************
* CompressExDecompressEx
* 168!\r\n结尾
* 16:8(!FastLZ )+4(maxout)+4()
* 8:4()+1(*)+1(CRC,,0)+2(\r\n)
* 20:
* 4(!FLZ)
* 4SecondsUnix时间戳Jan1st1970开始的秒数
* 4Milliseconds
* 4(maxout)
* 4()
* 8:
* 4
* 1(*)
* 1(CRC,,0)
* 2(\r\n)
******************************************************************************
* FastLZx86.dllFastLZx64.dll见项目 https://gitee.com/yhuse/SunnyUI.FastLZ
******************************************************************************/
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Sunny.UI
@ -168,9 +182,35 @@ namespace Sunny.UI
}
}
private static byte[] ExHead = "!FastLZ".ToEnBytes(8);
private const int ExHeadAllLength = 16;
private static byte[] ExHead = "!FLZ".ToEnBytes(4);
private const int ExHeadAllLength = 20;
private const int ExTailAllLength = 8;
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// 压缩(扩展)
/// </summary>
/// <param name="input">输入</param>
/// <param name="begin">起始位置</param>
/// <param name="length">长度</param>
/// <param name="datetime">时间</param>
/// <param name="index">索引</param>
/// <returns>压缩结果</returns>
public static byte[] CompressEx(byte[] input, int begin, int length, DateTime dateTime, int index)
{
byte[] result = CompressEx(input, begin, length);
if (result.Length > 0)
{
TimeSpan span = dateTime - Jan1st1970;
int totalSeconds = (int)span.TotalSeconds;
int milliseconds = span.Milliseconds;
Array.Copy(BitConverter.GetBytes(totalSeconds), 0, result, 4, 4);
Array.Copy(BitConverter.GetBytes(milliseconds), 0, result, 8, 4);
Array.Copy(BitConverter.GetBytes(index), 0, result, result.Length - 8, 4);
}
return result;
}
/// <summary>
/// 压缩(扩展)
@ -189,18 +229,21 @@ namespace Sunny.UI
{
int outlen = Is64bitApp() ? FastLZx64.FastLZ_Compress(pSrc1, length, pSrc2) : FastLZx86.FastLZ_Compress(pSrc1, length, pSrc2);
result = new byte[outlen + ExHeadAllLength + ExTailAllLength];
Array.Copy(ExHead, 0, result, 0, ExHead.Length);
Array.Copy(BitConverter.GetBytes((int)output.Length), 0, result, 8, 4);
Array.Copy(BitConverter.GetBytes((int)outlen), 0, result, 12, 4);
Array.Copy(output, 0, result, 16, outlen);
result[result.Length - 1 - 7] = 0; //保留
result[result.Length - 1 - 6] = 0; //保留
result[result.Length - 1 - 5] = 0; //保留
result[result.Length - 1 - 4] = 0; //保留
result[result.Length - 1 - 3] = 42; //*
result[result.Length - 1 - 2] = 0; //CRC
result[result.Length - 1 - 1] = 13; //\r
result[result.Length - 1 - 0] = 10; //\n
Array.Copy(BitConverter.GetBytes((int)0), 0, result, 4, 4);
Array.Copy(BitConverter.GetBytes((int)0), 0, result, 8, 4);
Array.Copy(BitConverter.GetBytes((int)output.Length), 0, result, 12, 4);
Array.Copy(BitConverter.GetBytes((int)outlen), 0, result, 16, 4);
Array.Copy(output, 0, result, ExHeadAllLength, outlen);
Array.Copy(BitConverter.GetBytes((int)0), 0, result, result.Length - 8, 4);
result[result.Length - 4] = 42; //*
result[result.Length - 3] = 0; //CRC
result[result.Length - 2] = 13; //\r
result[result.Length - 1] = 10; //\n
return result;
}
}
@ -219,7 +262,7 @@ namespace Sunny.UI
if (begin + length > input.Length) return result;
if (input[begin] != 33) return result;
if (input[begin + length - 4] != 42) return result;
if (length != BitConverter.ToInt32(input, begin + 12) + ExHeadAllLength + ExTailAllLength) return result;
if (length != BitConverter.ToInt32(input, begin + 16) + ExHeadAllLength + ExTailAllLength) return result;
byte[] output = new byte[BitConverter.ToInt32(input, begin + ExHead.Length)];
fixed (byte* pSrc1 = &input[begin + ExHeadAllLength])
@ -232,5 +275,67 @@ namespace Sunny.UI
return result;
}
}
/// <summary>
/// 解压缩(扩展)
/// </summary>
/// <param name="input">输入</param>
/// <param name="begin">起始位置</param>
/// <param name="length">长度</param>
/// <param name="datetime">时间</param>
/// <param name="index">索引</param>
/// <returns>解压缩结果</returns>
public static byte[] DecompressEx(byte[] input, int begin, int length, out DateTime datetime, out int index)
{
byte[] result = DecompressEx(input, begin, length);
datetime = Jan1st1970;
index = 0;
if (result.Length > 0)
{
datetime = Jan1st1970.AddSeconds(BitConverter.ToInt32(input, begin + 4)).AddMilliseconds(BitConverter.ToInt32(input, begin + 8));
index = BitConverter.ToInt32(input, begin + length - 8);
}
return result;
}
public static bool CheckFastLZDll()
{
if (File.Exists(DirEx.CurrentDir() + "FastLZx86.dll") && File.Exists(DirEx.CurrentDir() + "FastLZx64.dll")) return true;
try
{
CreateResourceToFile(DirEx.CurrentDir() + "FastLZx86.dll", "Sunny.UI.Common.FastLZx86.dat");
CreateResourceToFile(DirEx.CurrentDir() + "FastLZx64.dll", "Sunny.UI.Common.FastLZx64.dat");
}
catch
{
return false;
}
return File.Exists(DirEx.CurrentDir() + "FastLZx86.dll") && File.Exists(DirEx.CurrentDir() + "FastLZx64.dll");
}
/// <summary>
/// 从系统资源中保存字体文件
/// </summary>
/// <param name="file">字体文件名</param>
/// <param name="resource">资源名称</param>
private static void CreateResourceToFile(string file, string resource)
{
if (!File.Exists(file))
{
Stream fontStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
if (fontStream != null)
{
byte[] buffer = new byte[fontStream.Length];
fontStream.Read(buffer, 0, (int)fontStream.Length);
fontStream.Close();
File.WriteAllBytes(file, buffer);
}
}
}
}
}

View File

@ -36,6 +36,11 @@
<DocumentationFile></DocumentationFile>
</PropertyGroup>
<ItemGroup>
<None Remove="Common\FastLZx64.dat" />
<None Remove="Common\FastLZx86.dat" />
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
@ -48,6 +53,8 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Common\FastLZx64.dat" />
<EmbeddedResource Include="Common\FastLZx86.dat" />
<EmbeddedResource Include="Font\ElegantIcons.ttf" />
<EmbeddedResource Include="Font\fa-brands-400.ttf" />
<EmbeddedResource Include="Font\fa-regular-400.ttf" />