Add preemptive helper for Zstd handling

This commit is contained in:
Matt Nadareski
2025-10-08 12:04:04 -04:00
parent 2a6e066707
commit 16e80f75cf
2 changed files with 40 additions and 0 deletions

View File

@@ -36,6 +36,7 @@
- Fix default value tests
- Only allow skeleton creation for CD and DVD
- Add preemptive new file support
- Add preemptive helper for Zstd handling
### 3.4.2 (2025-09-30)

View File

@@ -8,6 +8,8 @@ using SabreTools.RedumpLib;
using SabreTools.RedumpLib.Data;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Archives.Zip;
using SharpCompress.Compressors;
using SharpCompress.Compressors.ZStandard;
#endif
namespace MPF.Processors
@@ -644,6 +646,43 @@ namespace MPF.Processors
#region Private Extra Methods
/// <summary>
/// Attempt to compress a file to Zstandard, removing the original on success
/// </summary>
/// <param name="file">Full path to an existing file</param>
/// <returns>True if the compression was a success, false otherwise</returns>
private static bool CompressZstandard(string file)
{
#if NET20 || NET35 || NET40 || NET452
// Compression is not available for this framework version
return false;
#else
// Ensure the file exists
if (!File.Exists(file))
return false;
// Create and write the output
try
{
using var ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var ofs = File.Open($"{file}.zst", FileMode.CreateNew, FileAccess.Write, FileShare.None);
using var zst = new ZStandardStream(ifs, CompressionMode.Compress, compressionLevel: 19);
zst.CopyTo(ofs);
ofs.Flush();
}
catch
{
return false;
}
// Try to delete the file
try { File.Delete(file); } catch { }
return true;
#endif
}
/// <summary>
/// Get if the datfile exists in the log
/// </summary>