mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-07-09 02:16:46 +00:00
Fixes folder extraction for 7z, RAR, and WinRAR SFX. Applied same fix to tar, but more is needed for it to work properly.
84 lines
2.7 KiB
C#
84 lines
2.7 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 string? Extract(string file, bool includeDebug)
|
|
{
|
|
if (!File.Exists(file))
|
|
return null;
|
|
|
|
using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
return Extract(fs, file, includeDebug);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public string? Extract(Stream? stream, string file, bool includeDebug)
|
|
{
|
|
if (stream == null)
|
|
return null;
|
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
|
try
|
|
{
|
|
// Create a temp output directory
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
Directory.CreateDirectory(tempPath);
|
|
|
|
using (TarArchive 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;
|
|
|
|
// TODO: Fix bug with extracting folders from tar.
|
|
string tempFile = Path.Combine(tempPath, 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 tempPath;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
return null;
|
|
}
|
|
#else
|
|
return null;
|
|
#endif
|
|
}
|
|
}
|
|
}
|