Files
BinaryObjectScanner/BinaryObjectScanner/FileType/BZip2.cs

61 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
2023-11-22 12:22:01 -05:00
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Compressors;
using SharpCompress.Compressors.BZip2;
2023-11-14 16:10:10 -05:00
#endif
namespace BinaryObjectScanner.FileType
{
/// <summary>
/// bzip2 archive
/// </summary>
2023-03-09 15:07:35 -05:00
public class BZip2 : IExtractable
{
/// <inheritdoc/>
public string? Extract(string file, bool includeDebug)
{
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);
}
/// <inheritdoc/>
2023-10-25 23:33:51 -04:00
public string? Extract(Stream? stream, string file, bool includeDebug)
{
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
}
}
}