Files
SabreTools.Serialization/SabreTools.Wrappers/LZQBasic.Extraction.cs

62 lines
2.0 KiB
C#
Raw Permalink Normal View History

2025-09-12 09:02:03 -04:00
using System;
using System.IO;
using SabreTools.IO.Compression.SZDD;
2026-03-18 16:37:59 -04:00
namespace SabreTools.Wrappers
2025-09-12 09:02:03 -04:00
{
public partial class LZQBasic : IExtractable
{
/// <inheritdoc/>
public bool Extract(string outputDirectory, bool includeDebug)
{
// Get the length of the compressed data
long compressedSize = Length - 12;
if (compressedSize < 12)
return false;
// Read in the data as an array
byte[]? contents = ReadRangeFromSource(12, (int)compressedSize);
2025-09-20 09:49:42 -04:00
if (contents.Length == 0)
2025-09-12 09:02:03 -04:00
return false;
// Get the decompressor
var decompressor = Decompressor.CreateQBasic(contents);
2026-01-25 14:30:18 -05:00
if (decompressor is null)
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 = "tempfile.bin";
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);
2025-09-12 09:02:03 -04:00
decompressor.CopyTo(fs);
fs.Flush();
}
catch (Exception ex)
{
if (includeDebug) Console.Error.WriteLine(ex);
return false;
}
return true;
}
}
}