Files
SabreTools.Serialization/SabreTools.Wrappers/GZip.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

75 lines
2.8 KiB
C#

using System;
using System.IO;
using SabreTools.Data.Models.GZIP;
using SabreTools.IO.Compression.Deflate;
using SabreTools.IO.Extensions;
#if NET20 || NET35
using SabreTools.Serialization;
#endif
namespace SabreTools.Wrappers
{
public partial class GZip : IExtractable
{
/// <inheritdoc/>
public bool Extract(string outputDirectory, bool includeDebug)
{
// Ensure there is data to extract
if (Header is null || DataOffset < 0)
{
if (includeDebug) Console.Error.WriteLine("Invalid archive detected, skipping...");
return false;
}
// Ensure that DEFLATE is being used
if (Header.CompressionMethod != CompressionMethod.Deflate)
{
if (includeDebug) Console.Error.WriteLine($"Invalid compression method {Header.CompressionMethod} detected, only DEFLATE is supported. Skipping...");
return false;
}
try
{
// Seek to the start of the compressed data
long offset = _dataSource.SeekIfPossible(DataOffset, SeekOrigin.Begin);
if (offset != DataOffset)
{
if (includeDebug) Console.Error.WriteLine($"Could not seek to compressed data at {DataOffset}");
return false;
}
// Ensure directory separators are consistent
string filename = Header.OriginalFileName
?? (Filename is not null ? Path.GetFileName(Filename).Replace(".gz", string.Empty) : null)
?? $"extracted_file";
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);
// Open the source as a DEFLATE stream
var deflateStream = new DeflateStream(_dataSource, CompressionMode.Decompress, leaveOpen: true);
// Write the file
using var fs = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.None);
deflateStream.CopyTo(fs);
fs.Flush();
return true;
}
catch (Exception ex)
{
if (includeDebug) Console.Error.WriteLine(ex);
return false;
}
}
}
}