diff --git a/ExtractionTool/Program.cs b/ExtractionTool/Program.cs index 1fc046f6..0cebd591 100644 --- a/ExtractionTool/Program.cs +++ b/ExtractionTool/Program.cs @@ -162,8 +162,7 @@ namespace ExtractionTool // Microsoft Cabinet archive case MicrosoftCabinet mscab: - Console.WriteLine("WARNING: LZX and Quantum compression schemes are not supported so some files may be skipped!"); - MicrosoftCabinet.ExtractSet(file, outputDirectory, includeDebug); + mscab.Extract(outputDirectory, includeDebug); break; // MoPaQ (MPQ) archive diff --git a/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs b/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs index d531c74c..6fdce386 100644 --- a/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs +++ b/SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs @@ -123,79 +123,29 @@ namespace SabreTools.Serialization.Wrappers #region Extraction - /// - /// Extract a cabinet set to an output directory, if possible - /// - /// Filename for one cabinet in the set - /// Path to the output directory - /// True to include debug data, false otherwise - /// Indicates if all files were able to be extracted - /// Will extract all items found in the set - public static bool ExtractSet(string filename, string outDir, bool includeDebug) - => ExtractSet(stream: null, filename, outDir, includeDebug); - - /// - /// Extract a cabinet set to an output directory, if possible - /// - /// Stream representing one cabinet in the set - /// Filename for one cabinet in the set - /// Path to the output directory - /// True to include debug data, false otherwise - /// Indicates if all files were able to be extracted - /// Will extract all items found in the set - public static bool ExtractSet(Stream? stream, string filename, string outDir, bool includeDebug) - { - // Get a wrapper for the set, if possible - MicrosoftCabinet? current; - if (File.Exists(filename)) - current = OpenSet(filename); - else - current = Create(stream); - - // Validate the header exists - if (current?.Model?.Header == null) - return false; - - try - { - // Loop through the cabinets - bool allExtracted = true; - do - { - allExtracted &= current.Extract(filename, outDir, includeDebug); - current = current.Next ?? current.OpenNext(filename); - } - while (current?.Header != null); - - return allExtracted; - } - catch (Exception ex) - { - if (includeDebug) Console.Error.WriteLine(ex); - return false; - } - } - /// public bool Extract(string outputDirectory, bool includeDebug) - => Extract(null, outputDirectory, includeDebug); - - /// - /// Filename for one cabinet in the set, if available - public bool Extract(string? filename, string outDir, bool includeDebug) { + // Display warning + Console.WriteLine("WARNING: LZX and Quantum compression schemes are not supported so some files may be skipped!"); + + // Open the full set if possible + var cabinet = this; + if (Filename != null) + cabinet = OpenSet(Filename); + // If the archive is invalid - if (Folders == null || Folders.Length == 0) + if (cabinet?.Folders == null || cabinet.Folders.Length == 0) return false; try { // Loop through the folders bool allExtracted = true; - for (int f = 0; f < Folders.Length; f++) + for (int f = 0; f < cabinet.Folders.Length; f++) { - var folder = Folders[f]; - allExtracted &= ExtractFolder(filename, outDir, folder, f, includeDebug); + var folder = cabinet.Folders[f]; + allExtracted &= cabinet.ExtractFolder(Filename, outputDirectory, folder, f, includeDebug); } return allExtracted; @@ -211,13 +161,13 @@ namespace SabreTools.Serialization.Wrappers /// Extract the contents of a single folder /// /// Filename for one cabinet in the set, if available - /// Path to the output directory + /// Path to the output directory /// Folder containing the blocks to decompress /// Index of the folder in the cabinet /// True to include debug data, false otherwise /// True if all files extracted, false otherwise private bool ExtractFolder(string? filename, - string outDir, + string outputDirectory, CFFOLDER? folder, int folderIndex, bool includeDebug) @@ -233,7 +183,7 @@ namespace SabreTools.Serialization.Wrappers for (int i = 0; i < files.Length; i++) { var file = files[i]; - allExtracted &= ExtractFile(outDir, blockStream, file, includeDebug); + allExtracted &= ExtractFile(outputDirectory, blockStream, file, includeDebug); } return allExtracted; @@ -242,12 +192,12 @@ namespace SabreTools.Serialization.Wrappers /// /// Extract the contents of a single file /// - /// Path to the output directory + /// Path to the output directory /// Stream representing the uncompressed block data /// File information /// True to include debug data, false otherwise /// True if the file extracted, false otherwise - private static bool ExtractFile(string outDir, Stream blockStream, CFFILE file, bool includeDebug) + private static bool ExtractFile(string outputDirectory, Stream blockStream, CFFILE file, bool includeDebug) { try { @@ -262,7 +212,7 @@ namespace SabreTools.Serialization.Wrappers filename = filename.Replace('\\', '/'); // Ensure the full output directory exists - filename = Path.Combine(outDir, filename); + filename = Path.Combine(outputDirectory, filename); var directoryName = Path.GetDirectoryName(filename); if (directoryName != null && !Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);