From be114f60d305066a896438d1ab9822bdbb09e360 Mon Sep 17 00:00:00 2001 From: TheRogueArchivist <24215969+TheRogueArchivist@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:18:00 -0600 Subject: [PATCH] Fix WinZip SFX folders not being scanned (#299) * Fix WinZip SFX folders not being scanned Use PKZIP extraction to fix WinZip SFX extraction not extracting folders. * Remove unneeded null check * Add checks for incomplete zip entries --- BinaryObjectScanner/FileType/PKZIP.cs | 4 ++++ BinaryObjectScanner/Packer/WinZipSFX.cs | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/BinaryObjectScanner/FileType/PKZIP.cs b/BinaryObjectScanner/FileType/PKZIP.cs index 8eb00e59..ca9f7e60 100644 --- a/BinaryObjectScanner/FileType/PKZIP.cs +++ b/BinaryObjectScanner/FileType/PKZIP.cs @@ -46,6 +46,10 @@ public class PKZIP : IExtractable if (entry.IsDirectory) continue; + // If we have a partial entry due to an incomplete multi-part archive, skip it + if (!entry.IsComplete) + continue; + string tempFile = Path.Combine(tempPath, entry.Key); var directoryName = Path.GetDirectoryName(tempFile); if (directoryName != null && !Directory.Exists(directoryName)) diff --git a/BinaryObjectScanner/Packer/WinZipSFX.cs b/BinaryObjectScanner/Packer/WinZipSFX.cs index 7fbf8c23..09c5e259 100644 --- a/BinaryObjectScanner/Packer/WinZipSFX.cs +++ b/BinaryObjectScanner/Packer/WinZipSFX.cs @@ -73,20 +73,18 @@ public class WinZipSFX : IExtractableNewExecutable, IExtractablePortableExecutab /// /// Handle common extraction between executable types /// - private static string? Extract(string file, bool includeDebug) + /// + public string? Extract(string file, bool includeDebug) { #if NET462_OR_GREATER || NETCOREAPP 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. + // Create a temp output directory + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(tempPath); + using (ZipArchive zipFile = ZipArchive.Open(file)) { - if (!zipFile.IsComplete) - return null; - - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tempPath); - foreach (var entry in zipFile.Entries) { try @@ -95,7 +93,14 @@ public class WinZipSFX : IExtractableNewExecutable, IExtractablePortableExecutab if (entry.IsDirectory) continue; + // If we have a partial entry due to an incomplete multi-part archive, skip it + if (!entry.IsComplete) + continue; + string tempFile = Path.Combine(tempPath, entry.Key); + var directoryName = Path.GetDirectoryName(tempFile); + if (directoryName != null && !Directory.Exists(directoryName)) + Directory.CreateDirectory(directoryName); entry.WriteToFile(tempFile); } catch (Exception ex) @@ -103,9 +108,9 @@ public class WinZipSFX : IExtractableNewExecutable, IExtractablePortableExecutab if (includeDebug) Console.WriteLine(ex); } } - - return tempPath; } + + return tempPath; } catch (Exception ex) {