using System; using System.IO; using SabreTools.Numerics.Extensions; namespace SabreTools.Wrappers { public partial class XZP : IExtractable { /// public bool Extract(string outputDirectory, bool includeDebug) { // If we have no directory entries if (DirectoryEntries.Length == 0) return false; // Loop through and extract all files to the output bool allExtracted = true; for (int i = 0; i < DirectoryEntries.Length; i++) { allExtracted &= ExtractFile(i, outputDirectory, includeDebug); } return allExtracted; } /// /// Extract a file from the XZP to an output directory by index /// /// File index to extract /// Output directory to write to /// True to include debug data, false otherwise /// True if the file extracted, false otherwise public bool ExtractFile(int index, string outputDirectory, bool includeDebug) { // If we have no directory entries if (DirectoryEntries.Length == 0) return false; // If we have no directory items if (DirectoryItems.Length == 0) return false; // If the directory entry index is invalid if (index < 0 || index >= DirectoryEntries.Length) return false; // Get the associated directory item var directoryEntry = DirectoryEntries[index]; var directoryItem = Array.Find(DirectoryItems, di => di?.FileNameCRC == directoryEntry.FileNameCRC); if (directoryItem is null) return false; // Load the item data var data = ReadRangeFromSource((int)directoryEntry.EntryOffset, (int)directoryEntry.EntryLength); if (data.Length == 0) return false; // If we have an invalid output directory if (string.IsNullOrEmpty(outputDirectory)) return false; // Ensure directory separators are consistent string filename = directoryItem.Name ?? $"file{index}"; if (Path.DirectorySeparatorChar == '\\') filename = filename.Replace('/', '\\'); else if (Path.DirectorySeparatorChar == '/') filename = filename.Replace('\\', '/'); // Ensure the full output directory exists filename = Path.Combine(outputDirectory, filename); var directoryName = Path.GetDirectoryName(filename); if (directoryName is not null && !Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName); // Try to write the data try { // Open the output file for writing using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None); fs.Write(data); fs.Flush(); } catch (Exception ex) { if (includeDebug) Console.Error.WriteLine(ex); return false; } return true; } } }