I have a method which was working fine with PDFs has started giving errors, I checked in TFS - I didn't find any changes in that part of the method, I am getting the error:
System.IO.InvalidDataException: 'End of Central Directory record could not be found.
But the PDF is valid and other clients told me it has started throwing errors for the pdfs previously were working. Here is the code:
try
{
using (MemoryStream ms = new MemoryStream(fileData))
{
List<string> dangerous_files = new List<string>();
string msgModel = "ZIP: {zip} - ({file})";
using (ZipArchive archive = new ZipArchive(ms))
{
foreach (var entity in archive.Entries)
{
if (denyExtensions.Contains(entity.FullName.Split('.').LastOrDefault().ToLower()))
{
dangerous_files.Add(msgModel.Replace("{zip}", GetRidOfSlash(file.FileName)).Replace("{file}", entity.FullName));
}
}
if (dangerous_files.Count > 0)
{
Response.TrySkipIisCustomErrors = true; Response.StatusCode = 500;
return Json(new
{
error = errMsg,
code = "500.1",
files = dangerous_files
});
}
}
}
}
catch (Exception ex)
{
LogHandler.LogError(4203, "Zip file creation failed Upload - Picture", ex);
throw ex;
}
The place where I am getting the errors is at the following line:
using (ZipArchive archive = new ZipArchive(ms))
Thoughts?