Skip to content

Commit

Permalink
Fix WinZip SFX folders not being scanned (#299)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TheRogueArchivist authored Apr 15, 2024
1 parent b2594f8 commit be114f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions BinaryObjectScanner/FileType/PKZIP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
25 changes: 15 additions & 10 deletions BinaryObjectScanner/Packer/WinZipSFX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,18 @@ public class WinZipSFX : IExtractableNewExecutable, IExtractablePortableExecutab
/// <summary>
/// Handle common extraction between executable types
/// </summary>
private static string? Extract(string file, bool includeDebug)
/// <inheritdoc/>
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
Expand All @@ -95,17 +93,24 @@ 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)
{
if (includeDebug) Console.WriteLine(ex);
}
}

return tempPath;
}

return tempPath;
}
catch (Exception ex)
{
Expand Down

0 comments on commit be114f6

Please sign in to comment.