From edb8c08a39879077626133fcfd9d1d91c59231a2 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 10 Oct 2025 10:07:32 -0400 Subject: [PATCH] Move Zstd compression helper to base processor --- CHANGELIST.md | 1 + MPF.Processors/BaseProcessor.cs | 53 +++++++++++++++++++++++++++++++++ MPF.Processors/Redumper.cs | 53 --------------------------------- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index fbdf622b..e5310117 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -1,6 +1,7 @@ ### WIP (xxxx-xx-xx) - Add BCA to list of files to select in Check UI +- Move Zstd compression helper to base processor ### 3.5.0 (2025-10-10) diff --git a/MPF.Processors/BaseProcessor.cs b/MPF.Processors/BaseProcessor.cs index 84285233..e772c87b 100644 --- a/MPF.Processors/BaseProcessor.cs +++ b/MPF.Processors/BaseProcessor.cs @@ -9,7 +9,9 @@ using SabreTools.RedumpLib.Data; using SharpCompress.Archives; using SharpCompress.Archives.Zip; using SharpCompress.Common; +using SharpCompress.Compressors; using SharpCompress.Compressors.Deflate; +using SharpCompress.Compressors.ZStandard; using SharpCompress.Writers.Zip; #endif @@ -563,6 +565,57 @@ namespace MPF.Processors #region Shared 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 + internal 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; + + try + { + // Prepare the input and output streams + using var ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var ofs = File.Open($"{file}.zst", FileMode.Create, FileAccess.Write, FileShare.None); + using var zst = new ZStandardStream(ofs, CompressionMode.Compress, compressionLevel: 19, leaveOpen: true); + + // Compress and write in blocks + int read = 0; + do + { + byte[] buffer = new byte[3 * 1024 * 1024]; + + read = ifs.Read(buffer, 0, buffer.Length); + if (read == 0) + break; + + zst.Write(buffer, 0, read); + zst.Flush(); + } while (read > 0); + } + catch + { + // Try to delete the incomplete + try { File.Delete($"{file}.zst"); } catch { } + + return false; + } + + // Try to delete the file + try { File.Delete(file); } catch { } + + return true; +#endif + } + /// /// Generate a CMP XML datfile string based on a single input file /// diff --git a/MPF.Processors/Redumper.cs b/MPF.Processors/Redumper.cs index af10666b..65d51dd2 100644 --- a/MPF.Processors/Redumper.cs +++ b/MPF.Processors/Redumper.cs @@ -8,8 +8,6 @@ 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 @@ -661,57 +659,6 @@ 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; - - try - { - // Prepare the input and output streams - using var ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - using var ofs = File.Open($"{file}.zst", FileMode.Create, FileAccess.Write, FileShare.None); - using var zst = new ZStandardStream(ofs, CompressionMode.Compress, compressionLevel: 19, leaveOpen: true); - - // Compress and write in blocks - int read = 0; - do - { - byte[] buffer = new byte[3 * 1024 * 1024]; - - read = ifs.Read(buffer, 0, buffer.Length); - if (read == 0) - break; - - zst.Write(buffer, 0, read); - zst.Flush(); - } while (read > 0); - } - catch - { - // Try to delete the incomplete - try { File.Delete($"{file}.zst"); } catch { } - - return false; - } - - // Try to delete the file - try { File.Delete(file); } catch { } - - return true; -#endif - } - /// /// Get if the datfile exists in the log ///