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

57 lines
2.0 KiB
C#
Raw Permalink Normal View History

2025-09-12 09:02:03 -04:00
using System;
using System.IO;
2026-08-27 09:36:42 -04:00
using Nanook.GrindCore;
using Nanook.GrindCore.BZip2;
2026-03-24 19:17:25 -04:00
using SabreTools.IO.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
{
/// <summary>
/// This is a shell wrapper; one that does not contain
/// any actual parsing. It is used as a placeholder for
/// types that typically do not have models.
/// </summary>
public partial class BZip2 : IExtractable
{
/// <inheritdoc/>
public bool Extract(string outputDirectory, bool includeDebug)
{
2026-01-25 14:30:18 -05:00
if (_dataSource is null || !_dataSource.CanRead)
2025-09-12 09:02:03 -04:00
return false;
try
{
// Try opening the stream
2026-08-27 09:36:42 -04:00
using var bz2File = new BZip2Stream(_dataSource, new CompressionOptions { LeaveOpen = true, Type = CompressionType.Decompress});
2025-09-12 09:02:03 -04:00
// Ensure directory separators are consistent
string filename = Guid.NewGuid().ToString();
filename = filename.TrimStart(['\\', '/']);
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);
// Extract the file
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
2026-03-24 19:17:25 -04:00
bz2File.BlockCopy(fs);
2025-09-12 09:02:03 -04:00
fs.Flush();
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.Error.WriteLine(ex);
return false;
}
}
}
}