ZipFile.TestArchive() returns true for empty file
Steps to reproduce
- Create Empty *.txt file and rename extension to *.zip
- 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;
}
}
Still repros on v1.4.2
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