Files
SabreTools.Serialization/SabreTools.Wrappers/LZKWAJ.Extraction.cs
Matt Nadareski 7689c6dd07 Libraries
This change looks dramatic, but it's just separating out the already-split namespaces into separate top-level folders. In theory, every single one could be built into their own Nuget package. `SabreTools.Serialization` still builds the normal Nuget package that is used by all other projects and includes all namespaces.
2026-03-21 16:26:56 -04:00

66 lines
2.2 KiB
C#

using System;
using System.IO;
using SabreTools.IO.Compression.SZDD;
namespace SabreTools.Wrappers
{
public partial class LZKWAJ : IExtractable
{
/// <inheritdoc/>
public bool Extract(string outputDirectory, bool includeDebug)
{
// Get the length of the compressed data
long compressedSize = Length - DataOffset;
if (compressedSize < DataOffset)
return false;
// Read in the data as an array
byte[]? contents = ReadRangeFromSource(DataOffset, (int)compressedSize);
if (contents.Length == 0)
return false;
// Get the decompressor
var decompressor = Decompressor.CreateKWAJ(contents, (ushort)CompressionType);
if (decompressor is null)
return false;
// If we have an invalid output directory
if (string.IsNullOrEmpty(outputDirectory))
return false;
// Create the full output path
string filename = FileName ?? "tempfile";
if (FileExtension is not null)
filename += $".{FileExtension}";
// Ensure directory separators are consistent
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);
decompressor.CopyTo(fs);
fs.Flush();
}
catch (Exception ex)
{
if (includeDebug) Console.Error.WriteLine(ex);
return false;
}
return true;
}
}
}