2

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?

elmer007
  • 1,412
  • 14
  • 27
  • Has the pdf or zip gotten corrupted during a download? – ΩmegaMan Jan 02 '20 at 19:18
  • No as I said in the question its all good pdf – user2243643 Jan 02 '20 at 19:19
  • Seems weird to me that you're unpacking the file as if it's a zip file when you said you are taking in pdfs – Austin Arnett Jan 02 '20 at 19:20
  • Whatever you're trying to do with `.pdf` files should not happening here. This is looking at a zipped file and checking if anything is "dangerous", which is determined by the extension name of each file inside of the zip file. If a pdf is entering this method you're missing a condition somewhere above this entire thing to see if it's a pdf – Austin Arnett Jan 02 '20 at 19:30
  • Yes Austin - but yes the condition is if the file is not of image like gif, png jpg then execute this, but why is this thing failing - ZipArchive archive = new ZipArchive(ms) - any idea? – user2243643 Jan 02 '20 at 19:46
  • Without the actual file example to test off of, its hard for us on SO to determine an actual issue. Even you said the code worked originally. Unless you can create an example file which fails in the same way and can shown via a code sample here on the post...not much can be done by us. – ΩmegaMan Jan 02 '20 at 20:10
  • Yes I understand this part of the function is just collecting the suspicious (or dangerous) file extensions and zipping the files using ZipArchive, but why is it failing, what can I do to avoid it? Thank you – user2243643 Jan 02 '20 at 23:05

0 Answers0