Skip to content

Better handling of Zip files without a valid signature found - #1479

Merged
doug24 merged 10 commits into
dnGrep:masterfrom
azerios:master
May 19, 2026
Merged

doug24 merged 10 commits into
dnGrep:masterfrom
azerios:master

Conversation

@azerios

@azerios azerios commented May 18, 2026

Copy link
Copy Markdown
Contributor

The goal of the PR is to fix this error in Grep_Error_Log.xml

Failed to search inside archive 'C:\PROJECTS\client.zip' System.ArgumentException: The stream is invalid or no corresponding signature was found. at SevenZip.FileChecker.CheckSignature(Stream stream, Int32& offset, Boolean& isExecutable) in C:\Users\tbernard\source\repos\dnGrep\SevenZip\FileChecker.cs:line 230 at SevenZip.SevenZipExtractor.Init(Stream stream, Boolean leaveOpen) in C:\Users\tbernard\source\repos\dnGrep\SevenZip\SevenZipExtractor.cs:line 113 at SevenZip.SevenZipExtractor..ctor(Stream archiveStream, String password, Boolean leaveOpen) in C:\Users\tbernard\source\repos\dnGrep\SevenZip\SevenZipExtractor.cs:line 238 at dnGREP.Engines.ArchiveEngine.SearchInsideArchive(Stream input, String fileName, String password, String searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding, PauseCancelToken pauseCancelToken)+MoveNext() in C:\Users\tbernard\source\repos\dnGrep\dnGREP.Engines\ArchiveEngine.cs:line 141 at dnGREP.Engines.ArchiveEngine.Search(Stream input, String fileName, String password, String searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding, PauseCancelToken pauseCancelToken)+MoveNext() in C:\Users\tbernard\source\repos\dnGrep\dnGREP.Engines\ArchiveEngine.cs:line 108

What changed and why
Before
new SevenZipExtractor(input, password, true) — format guessed from stream only
After
Format is resolved first with the same try/catch fallback to extension that the string constructor uses

Before
ArgumentException propagates when no signature matches
After
Falls back to Formats.FormatByFileName(fileName, true) using the known fileName

Before
Uses the (Stream, string, bool) overload which calls CheckSignature(Stream) internally
After
Uses the (Stream, InArchiveFormat, bool) / (Stream, string, InArchiveFormat, bool) overload which skips CheckSignature entirely

@doug24

doug24 commented May 18, 2026

Copy link
Copy Markdown
Contributor

Thank-you for the submission. I'm not opposed to making a change like this, but I think there is a better way to do it.

First, I have to ask why this is necessary - what application or library is creating zip files without a valid zip signature? I don't think this has ever come up before in dnGrep.

Second, you made this change in one place, ArchiveEngine.SearchInsideArchive. There are three other places in the dnGrep code that are using the same SevenZipExtractor constructor: ArchiveDirectory.EnumerateFiles, ArchiveDirectory.GetLinesWithContext, and ArchiveDirectory.ExtractToTempFile. Wouldn't those methods also need to check the file name when the signature fails?

I think it would be better to leave calling the FileChecker code in the SevenZip library and not copy it to each of these callers. I would add a new SevenZipExtactor constructor that takes the archive name in addition to the stream:

        public SevenZipExtractor(string archiveName, Stream archiveStream, string password, bool leaveOpen = false)

Then add an optional parameter to the Init method:

        private void Init(Stream stream, bool leaveOpen = false, string archiveName = null)

And add a new CheckSignature method to FileChecker that takes both a file name and a stream:

        /// <summary>
        /// Gets the InArchiveFormat for a specific extension.
        /// </summary>
        /// <param name="fileName">The archive file name (optional)</param>
        /// <param name="stream">The stream to identify.</param>
        /// <param name="offset">The archive beginning offset.</param>
        /// <param name="isExecutable">True if the original format of the stream is PE; otherwise, false.</param>
        /// <returns>Corresponding InArchiveFormat.</returns>
        public static InArchiveFormat CheckSignature(string fileName, Stream stream, out int offset, out bool isExecutable)
        {
            try
            {
                return CheckSignature(stream, out offset, out isExecutable);
            }
            catch (ArgumentException)
            {
                if (!string.IsNullOrEmpty(fileName))
                {
                    offset = 0;
                    isExecutable = false;
                    return Formats.FormatByFileName(fileName, true);
                }
                throw;
            }
        }

GetLinesWithContext and ExtractToTempFile do not currently have the archive file name, it will have to be passed down the calling tree.

@azerios

azerios commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

I applied these changes

@doug24

doug24 commented May 19, 2026

Copy link
Copy Markdown
Contributor

Thank-you. I'm merging this pull request.

I think you missed my second comment above: this change should also be applied to the ArchiveDirectory class. I'll add another PR to make those changes

@doug24
doug24 merged commit a7f8260 into dnGrep:master May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants