Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
Please check below, Its resolved my issue https://www.carlrippon.com/zipping-up-files-from-a-memorystream/
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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();
}
Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Please check below, Its resolved my issue https://www.carlrippon.com/zipping-up-files-from-a-memorystream/
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();
}
}