2020-09-10 21:10:32 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2023-03-09 14:04:31 -05:00
|
|
|
|
using BinaryObjectScanner.Interfaces;
|
2023-11-22 12:22:01 -05:00
|
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using SharpCompress.Compressors;
|
|
|
|
|
|
using SharpCompress.Compressors.BZip2;
|
2023-11-14 16:10:10 -05:00
|
|
|
|
#endif
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2023-03-10 13:48:24 -05:00
|
|
|
|
namespace BinaryObjectScanner.FileType
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2022-12-08 21:32:52 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// bzip2 archive
|
|
|
|
|
|
/// </summary>
|
2023-03-09 15:07:35 -05:00
|
|
|
|
public class BZip2 : IExtractable
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2023-03-09 14:04:31 -05:00
|
|
|
|
/// <inheritdoc/>
|
2023-09-17 00:18:04 -04:00
|
|
|
|
public string? Extract(string file, bool includeDebug)
|
2023-03-09 14:04:31 -05:00
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2023-12-13 15:52:03 -05:00
|
|
|
|
using var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
2023-11-22 13:28:13 -05:00
|
|
|
|
return Extract(fs, file, includeDebug);
|
2023-03-09 14:04:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2023-10-25 23:33:51 -04:00
|
|
|
|
public string? Extract(Stream? stream, string file, bool includeDebug)
|
2023-03-09 14:04:31 -05:00
|
|
|
|
{
|
2023-10-25 23:33:51 -04:00
|
|
|
|
if (stream == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2023-11-22 12:22:01 -05:00
|
|
|
|
#if NET462_OR_GREATER || NETCOREAPP
|
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);
|
|
|
|
|
|
|
2024-11-04 10:20:46 -05:00
|
|
|
|
using (var bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true))
|
2023-03-09 14:39:26 -05:00
|
|
|
|
{
|
2023-03-09 17:16:39 -05:00
|
|
|
|
string tempFile = Path.Combine(tempPath, Guid.NewGuid().ToString());
|
|
|
|
|
|
using (FileStream fs = File.OpenWrite(tempFile))
|
|
|
|
|
|
{
|
|
|
|
|
|
bz2File.CopyTo(fs);
|
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
}
|
2023-11-14 16:10:10 -05:00
|
|
|
|
#else
|
|
|
|
|
|
return null;
|
|
|
|
|
|
#endif
|
2023-03-09 14:04:31 -05:00
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|