mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-02-09 13:47:42 +00:00
87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using BinaryObjectScanner.Interfaces;
|
|
using BinaryObjectScanner.Wrappers;
|
|
using SharpCompress.Archives;
|
|
using SharpCompress.Archives.Rar;
|
|
using SharpCompress.Readers;
|
|
|
|
namespace BinaryObjectScanner.Packer
|
|
{
|
|
public class WinRARSFX : IExtractable, IPortableExecutableCheck
|
|
{
|
|
/// <inheritdoc/>
|
|
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
|
|
{
|
|
// Get the sections from the executable, if possible
|
|
var sections = pex?.SectionTable;
|
|
if (sections == null)
|
|
return null;
|
|
|
|
string name = pex.AssemblyDescription;
|
|
if (name?.Contains("WinRAR archiver") == true)
|
|
return "WinRAR SFX";
|
|
|
|
var resources = pex.FindDialogByTitle("WinRAR self-extracting archive");
|
|
if (resources.Any())
|
|
return "WinRAR SFX";
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <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.Read))
|
|
{
|
|
return Extract(fs, file, includeDebug);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public string Extract(Stream stream, string file, bool includeDebug)
|
|
{
|
|
try
|
|
{
|
|
// Should be using stream instead of file, but stream fails to extract anything. My guess is that the executable portion of the archive is causing stream to fail, but not file.
|
|
using (RarArchive zipFile = RarArchive.Open(file, new ReaderOptions() { LookForHeader = true }))
|
|
{
|
|
if (!zipFile.IsComplete)
|
|
return null;
|
|
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
Directory.CreateDirectory(tempPath);
|
|
|
|
foreach (var entry in zipFile.Entries)
|
|
{
|
|
try
|
|
{
|
|
// If we have a directory, skip it
|
|
if (entry.IsDirectory)
|
|
continue;
|
|
|
|
string tempFile = Path.Combine(tempPath, entry.Key);
|
|
entry.WriteToFile(tempFile);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
}
|
|
}
|
|
|
|
return tempPath;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (includeDebug) Console.WriteLine(ex);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|