mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-02-13 21:31:04 +00:00
78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using BinaryObjectScanner.Interfaces;
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
|
using SharpCompress.Archives;
|
|
using SharpCompress.Archives.Tar;
|
|
#endif
|
|
|
|
namespace BinaryObjectScanner.FileType
|
|
{
|
|
/// <summary>
|
|
/// Tape archive
|
|
/// </summary>
|
|
public class TapeArchive : IExtractable
|
|
{
|
|
/// <inheritdoc/>
|
|
public bool Extract(string file, string outDir, bool includeDebug)
|
|
{
|
|
if (!File.Exists(file))
|
|
return false;
|
|
|
|
using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
return Extract(fs, file, outDir, includeDebug);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public bool Extract(Stream? stream, string file, string outDir, bool includeDebug)
|
|
{
|
|
if (stream == null || !stream.CanRead)
|
|
return false;
|
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
|
try
|
|
{
|
|
using var tarFile = TarArchive.Open(stream);
|
|
foreach (var entry in tarFile.Entries)
|
|
{
|
|
try
|
|
{
|
|
// If the entry is a directory
|
|
if (entry.IsDirectory)
|
|
continue;
|
|
|
|
// If the entry has an invalid key
|
|
if (entry.Key == null)
|
|
continue;
|
|
|
|
// If we have a partial entry due to an incomplete multi-part archive, skip it
|
|
if (!entry.IsComplete)
|
|
continue;
|
|
|
|
string tempFile = Path.Combine(outDir, entry.Key);
|
|
var directoryName = Path.GetDirectoryName(tempFile);
|
|
if (directoryName != null && !Directory.Exists(directoryName))
|
|
Directory.CreateDirectory(directoryName);
|
|
|
|
entry.WriteToFile(tempFile);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
return false;
|
|
}
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
}
|
|
}
|