1
0
mirror of synced 2024-11-27 17:00:50 +01:00

Support external zsdic files if user puts zsdic files in Lib/ZstdDictionaries

This commit is contained in:
KillzXGaming 2023-05-14 15:37:14 -04:00
parent c9e74e0be1
commit e59bfe10a8

View File

@ -5,6 +5,7 @@ using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Toolbox.Library.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace Toolbox.Library
{
@ -15,8 +16,12 @@ namespace Toolbox.Library
public override string ToString() { return "ZSTD"; }
static string fileNameTemp = "";
public bool Identify(Stream stream, string fileName)
{
//Small hack to check current file name
fileNameTemp = fileName;
using (var reader = new FileReader(stream, true))
{
uint magic = reader.ReadUInt32();
@ -39,14 +44,26 @@ namespace Toolbox.Library
public static byte[] SDecompress(byte[] b)
{
using (var decompressor = new ZstdNet.Decompressor())
var options = new ZstdNet.DecompressionOptions(GetExternalDictionaries());
using (var decompressor = new ZstdNet.Decompressor(options))
{
return decompressor.Unwrap(b);
}
}
public static byte[] SDecompress(byte[] b, byte[] dict)
{
var options = new ZstdNet.DecompressionOptions(dict);
using (var decompressor = new ZstdNet.Decompressor(options))
{
return decompressor.Unwrap(b);
}
}
public static byte[] SDecompress(byte[] b, int MaxDecompressedSize)
{
using (var decompressor = new ZstdNet.Decompressor())
var options = new ZstdNet.DecompressionOptions(GetExternalDictionaries());
using (var decompressor = new ZstdNet.Decompressor(options))
{
return decompressor.Unwrap(b, MaxDecompressedSize);
}
@ -58,5 +75,31 @@ namespace Toolbox.Library
return compressor.Wrap(b);
}
}
static byte[] GetExternalDictionaries()
{
byte[] dictionary = new byte[0];
string folder = Path.Combine(Runtime.ExecutableDir, "Lib", "ZstdDictionaries");
if (Directory.Exists(folder))
{
void CheckZDic(string fileName, string expectedExtension)
{
//Dictionary already set
if (dictionary.Length != 0) return;
string zDictPath = Path.Combine(folder, fileName);
//Then check if the input file uses the expected extension
if (File.Exists(zDictPath) && fileNameTemp.EndsWith(expectedExtension))
dictionary = File.ReadAllBytes(zDictPath);
}
//Order matters, zs must go last
CheckZDic("bcett.byml.zsdic", "bcett.byml.zs" );
CheckZDic("pack.zsdic", "pack.zs" );
CheckZDic("zs.zsdic", ".zs" );
}
return dictionary;
}
}
}