mirror of
https://github.com/SabreTools/MPF.git
synced 2026-02-04 05:35:52 +00:00
Add preemptive helper for Zstd handling
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user