diff --git a/CHANGELIST.md b/CHANGELIST.md index 4c228141..1f0f3219 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -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) diff --git a/MPF.Processors/Redumper.cs b/MPF.Processors/Redumper.cs index 5dda5bb0..9bc14866 100644 --- a/MPF.Processors/Redumper.cs +++ b/MPF.Processors/Redumper.cs @@ -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 + /// + /// Attempt to compress a file to Zstandard, removing the original on success + /// + /// Full path to an existing file + /// True if the compression was a success, false otherwise + 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 + } + /// /// Get if the datfile exists in the log ///