Enable bz2 and gzip on all platforms

This commit is contained in:
Matt Nadareski
2024-12-13 14:07:08 -05:00
parent f9b4e262f2
commit 103ffc70bd
2 changed files with 11 additions and 40 deletions

View File

@@ -1,10 +1,7 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Compressors;
using SharpCompress.Compressors.BZip2;
#endif
using SabreTools.Compression.BZip2;
namespace BinaryObjectScanner.FileType
{
@@ -29,11 +26,10 @@ namespace BinaryObjectScanner.FileType
if (stream == null || !stream.CanRead)
return false;
#if NET462_OR_GREATER || NETCOREAPP
try
{
// Try opening the stream
using var bz2File = new BZip2Stream(stream, CompressionMode.Decompress, true);
using var bz2File = new BZip2InputStream(stream, true);
// Create the output file path
Directory.CreateDirectory(outDir);
@@ -50,9 +46,6 @@ namespace BinaryObjectScanner.FileType
if (includeDebug) Console.WriteLine(ex);
return false;
}
#else
return false;
#endif
}
}
}

View File

@@ -1,10 +1,7 @@
using System;
using System.IO;
using BinaryObjectScanner.Interfaces;
#if NET462_OR_GREATER || NETCOREAPP
using SharpCompress.Archives;
using SharpCompress.Archives.GZip;
#endif
using SabreTools.Compression.Deflate;
namespace BinaryObjectScanner.FileType
{
@@ -29,34 +26,18 @@ namespace BinaryObjectScanner.FileType
if (stream == null || !stream.CanRead)
return false;
#if NET462_OR_GREATER || NETCOREAPP
try
{
using var zipFile = GZipArchive.Open(stream);
foreach (var entry in zipFile.Entries)
{
try
{
// If the entry is a directory
if (entry.IsDirectory)
continue;
// Try opening the stream
using var gzipFile = new GZipStream(stream, CompressionMode.Decompress, true);
// If the entry has an invalid key
if (entry.Key == null)
continue;
// Create the output file path
Directory.CreateDirectory(outDir);
string tempFile = Path.Combine(outDir, Guid.NewGuid().ToString());
string tempFile = Path.Combine(outDir, 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);
}
}
// Extract the file
using FileStream fs = File.OpenWrite(tempFile);
gzipFile.CopyTo(fs);
return true;
}
@@ -65,9 +46,6 @@ namespace BinaryObjectScanner.FileType
if (includeDebug) Console.WriteLine(ex);
return false;
}
#else
return false;
#endif
}
}
}