Files
BinaryObjectScanner/BinaryObjectScanner.FileType/InstallShieldArchiveV3.cs

69 lines
2.3 KiB
C#
Raw Normal View History

2021-07-21 13:40:32 -07:00
using System;
using System.IO;
using System.Linq;
using BinaryObjectScanner.Interfaces;
2021-07-21 13:40:32 -07:00
using UnshieldSharp.Archive;
namespace BinaryObjectScanner.FileType
2021-07-21 13:40:32 -07:00
{
/// <summary>
/// InstallShield archive v3
/// </summary>
2023-03-09 15:07:35 -05:00
public class InstallShieldArchiveV3 : IExtractable
2021-07-21 13:40:32 -07:00
{
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
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))
{
2023-03-09 17:16:39 -05:00
return Extract(fs, file, includeDebug);
}
}
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
public string Extract(Stream stream, string file, bool includeDebug)
{
2023-03-09 17:16:39 -05:00
try
2023-03-09 14:39:26 -05:00
{
2023-03-09 17:16:39 -05:00
// Create a temp output directory
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
2023-03-09 14:39:26 -05:00
2023-03-09 17:16:39 -05:00
UnshieldSharp.Archive.InstallShieldArchiveV3 archive = new UnshieldSharp.Archive.InstallShieldArchiveV3(file);
foreach (CompressedFile cfile in archive.Files.Select(kvp => kvp.Value))
2023-03-09 14:39:26 -05:00
{
2023-03-09 17:16:39 -05:00
try
{
string tempFile = Path.Combine(tempPath, cfile.FullPath);
if (!Directory.Exists(Path.GetDirectoryName(tempFile)))
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
(byte[] fileContents, string error) = archive.Extract(cfile.FullPath);
if (!string.IsNullOrWhiteSpace(error))
continue;
using (FileStream fs = File.OpenWrite(tempFile))
{
fs.Write(fileContents, 0, fileContents.Length);
}
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
}
2023-03-09 14:39:26 -05:00
}
2023-03-09 17:16:39 -05:00
return tempPath;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
return null;
}
}
2021-07-21 13:40:32 -07:00
}
}