C# - System.IO.InvalidDataException: 'End of Central Directory record could not be found.'

Spamzilla 41 Reputation points
2022-12-21T23:58:47.38+00:00

Basically, I have a program that reads a .ZIP file then saves it back to the hard disc. Although C# crashes with the 'System.IO.InvalidDataException: 'End of Central Directory record could not be found.' a program called 7-Zip opens and extracts valid data from this archive. My code is similar to this:

byte[][] jaggedDataArray;  
  
private void Open_ZIP_File(string tempFileName)  
{  
         System.IO.FileStream tempZipFileStream = new System.IO.FileStream(tempFileName, System.IO.FileMode.Open);  
         ZipArchive tempZipArchive = new ZipArchive(tempZipFileStream, ZipArchiveMode.Read);	// This is where it crashes the second time  
  
         jaggedDataArray = new byte[tempZipArchive.Entries.Count][];  
  
         myListView.BeginUpdate();  
  
         foreach (ZipArchiveEntry tempZAE in tempZipArchive.Entries)  
         {  
             myListView.Items.Add(tempZAE.FullName);  
                 
             jaggedDataArray[myListView.Items.Count - 1] = new byte[tempZAE.Length];  
  
             System.IO.Stream tempStream = tempZAE.Open();  
  
             tempStream.Read(jaggedDataArray[myListView.Items.Count - 1], 0, (int)tempZAE.Length);  
         }  
  
         myListView.EndUpdate();  
         tempZipFileStream.Close();  
}  
  
  
private void Save_ZIP_File(object sender, EventArgs e)  
{         
       System.IO.FileStream tempZipFileStream = new System.IO.FileStream(".\\Test.ZIP", System.IO.FileMode.Create);  
       ZipArchive tempZipArchive = new ZipArchive(tempZipFileStream, ZipArchiveMode.Create);  
       ZipArchiveEntry tempZAE;  
  
       for (int ii = 0; ii < myListView.Items.Count - 1; ii++)  
       {  
  
           tempZAE = tempZipArchive.CreateEntry(myListView.Text);  
  
           System.IO.BinaryWriter tempBW = new System.IO.BinaryWriter(tempZAE.Open());  
  
           tempBW.Write(jaggedDataArray[ii]);  
  
           tempBW.Close();  
                     
       }  
  
       tempZipFileStream.Close();  
  
}  
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Spamzilla 41 Reputation points
    2022-12-22T16:30:23.777+00:00

    I found that when I used the 'using' keyword as in this link (https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchiveentry?view=netframework-4.7.2) that the file saving/loading worked.

     private void Save_ZIP_File(object sender, EventArgs e)  
     {         
            using (System.IO.FileStream tempZipFileStream = new System.IO.FileStream(".\\Test.ZIP", System.IO.FileMode.Create))  
     {  
     using (ZipArchive tempZipArchive = new ZipArchive(tempZipFileStream, ZipArchiveMode.Create);)  
     {  
             ZipArchiveEntry tempZAE;  
      
     for (int ii = 0; ii < myListView.Items.Count - 1; ii++)  
             {  
                tempZAE = tempZipArchive.CreateEntry(myListView.Items[ii].Text);  
          
                 using (System.IO.BinaryWriter tempBW = new System.IO.BinaryWriter(tempZAE.Open()))  
     {  
                  tempBW.Write(jaggedDataArray[ii]);  
             tempBW.Close();      
     }  
             }  
     }  
      
     tempZipFileStream.Close();  
     }  
     }  
    
    0 comments No comments

  2. Agrawal, Saurabh 0 Reputation points
    2024-02-19T07:53:07.25+00:00

    Please check below, Its resolved my issue https://www.carlrippon.com/zipping-up-files-from-a-memorystream/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.