Files

94 lines
3.4 KiB
C#
Raw Permalink Normal View History

2025-09-12 09:02:03 -04:00
using System;
using System.IO;
2026-04-19 09:33:55 -04:00
using SabreTools.Numerics.Extensions;
2025-09-12 09:02:03 -04:00
2026-03-18 16:37:59 -04:00
namespace SabreTools.Wrappers
2025-09-12 09:02:03 -04:00
{
public partial class XZP : IExtractable
{
/// <inheritdoc/>
public bool Extract(string outputDirectory, bool includeDebug)
{
// If we have no directory entries
2025-10-30 23:59:08 -04:00
if (DirectoryEntries.Length == 0)
2025-09-12 09:02:03 -04:00
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;
}
/// <summary>
/// Extract a file from the XZP to an output directory by index
/// </summary>
/// <param name="index">File index to extract</param>
/// <param name="outputDirectory">Output directory to write to</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>True if the file extracted, false otherwise</returns>
public bool ExtractFile(int index, string outputDirectory, bool includeDebug)
{
// If we have no directory entries
2025-10-30 23:59:08 -04:00
if (DirectoryEntries.Length == 0)
2025-09-12 09:02:03 -04:00
return false;
// If we have no directory items
2025-10-30 23:59:08 -04:00
if (DirectoryItems.Length == 0)
2025-09-12 09:02:03 -04:00
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);
2026-01-25 14:30:18 -05:00
if (directoryItem is null)
2025-09-12 09:02:03 -04:00
return false;
// Load the item data
var data = ReadRangeFromSource((int)directoryEntry.EntryOffset, (int)directoryEntry.EntryLength);
2025-09-20 09:49:42 -04:00
if (data.Length == 0)
2025-09-12 09:02:03 -04:00
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}";
filename = filename.TrimStart(['\\', '/']);
2025-09-12 09:02:03 -04:00
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);
2026-01-25 14:32:49 -05:00
if (directoryName is not null && !Directory.Exists(directoryName))
2025-09-12 09:02:03 -04:00
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);
2026-04-19 09:33:55 -04:00
fs.Write(data);
2025-09-12 09:02:03 -04:00
fs.Flush();
}
catch (Exception ex)
{
if (includeDebug) Console.Error.WriteLine(ex);
return false;
}
return true;
}
}
}