SharpZipLib icon indicating copy to clipboard operation
SharpZipLib copied to clipboard

ZipFile.TestArchive() returns true for empty file

Open mochaloff opened this issue 3 years ago • 2 comments

Steps to reproduce

  1. Create Empty *.txt file and rename extension to *.zip
  2. Use OpenReadStream() and TestArchive(true, TestStrategy.FindFirstError, null)

Expected behavior

The method must return false

Actual behavior

Method returns true

Version of SharpZipLib

1.3.3

Obtained from (only keep the relevant lines)

  • Package installed using NuGet

Notably, the method returns false if there is any information in the text file

My code:

public static bool IsCorrectZipFile(this IFormFile zipFile) //zipFile = emptyTxtFile.zip
{
    try
    {
        bool result;

        using (ZipFile zip = new ZipFile(zipFile.OpenReadStream()))
        {
            result = zip.TestArchive(true, TestStrategy.FindFirstError, null);
        }
        return result;
    }
    catch
    {
        return false;
    }
}

mochaloff avatar Feb 13 '22 23:02 mochaloff

Still repros on v1.4.2

y0ung3r avatar Jun 15 '24 17:06 y0ung3r

I don't think it's a bug.

Let's look into the ZipFile constructor: https://github.com/icsharpcode/SharpZipLib/blob/ff2d7c30bdb2474d507f001bc555405e9f02a0bb/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L581-L597

It becomes clear that any empty file with the .zip extension is perfectly valid. For such files the isNewArchive_ flag is set to true.

Now let's look into the source code of the TestArchive method and find out that if the archive is empty, there is nothing to check. https://github.com/icsharpcode/SharpZipLib/blob/ff2d7c30bdb2474d507f001bc555405e9f02a0bb/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L1037

And if there is nothing to check, the archive contains no errors: https://github.com/icsharpcode/SharpZipLib/blob/ff2d7c30bdb2474d507f001bc555405e9f02a0bb/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L1158

y0ung3r avatar Jun 15 '24 17:06 y0ung3r