Ziparchive Creates Invalid Zip File

ZipArchive creates invalid ZIP file

I found the—in hindsight, obvious—error in my code. The ZipArchive has to be disposed to make it write its content to its underlying stream. So I had to save the stream to a file after the end of the using block of the ZipArchive.

And it was important to set the leaveOpen argument of its constructor to true, to make it not close the underlying stream. So here is the complete working solution:

using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
var entry = zip.CreateEntry("test.txt");
using (StreamWriter sw = new StreamWriter(entry.Open()))
{
sw.WriteLine(
"Etiam eros nunc, hendrerit nec malesuada vitae, pretium at ligula.");
}
}

var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
"test.zip",
CreationCollisionOption.ReplaceExisting);

zipStream.Position = 0;
using (Stream s = await file.OpenStreamForWriteAsync())
{
zipStream.CopyTo(s);
}
}

ZipArchive Invalid Zip File

Change line instantiating archive:

using var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true);

To:

using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))

Otherwise you have you streams disposal mixed up which leads to described behaviour.

Invalid zip file after creating it with System.IO.Compression

You need to get the MemoryStream buffer via ToArray after the ZipArchive object gets disposed. Otherwise you end up with corrupted archive.

And please note that I have changed the parameters of ZipArchive constructor to keep it open when adding entries.

There is some checksumming going on when the ZipArchive is beeing disposed so if you read the MemoryStream before, it is still incomplete.

    private FileResult CreateZip(IEnumerable<FileContentResult> files)
{
byte[] retVal = null;

if (files.Any())
{
using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
foreach (var f in files)
{
var entry = archive.CreateEntry(f.FileDownloadName, CompressionLevel.Fastest);
using (BinaryWriter writer = new BinaryWriter(entry.Open()))
{
writer.Write(f.FileContents, 0, f.FileContents.Length);
writer.Close();
}
}

zipStream.Position = 0;
}
retVal = zipStream.ToArray();
}
}

return File(retVal, MediaTypeNames.Application.Zip, "horta.zip");
}

ZipArchive generated zip file not extracted

Using statement braces matter here.

You have to flush out any buffered data and finish the zip archive, by closing it, which is what writes the central directory record into the zip file, before you read back the bytes written to the MemoryStream.

ZipArchive serves up invalid file on live server

So the fix for this question was simply to change the byte[] bytes = MemoryStream.GetBuffer(); to byte[] bytes = MemoryStream.ToArray(); What this does is only get the used bytes not the extra bytes the buffer adds.

unable to create a valid zip file in php using zip archive?

The major reason for this was that there was huge zip file already existing in the same folder with the zip name I used to create a new zip thus due to some reason the new zip was not created and this old huge zip begined to download and as suggested by Marcelo in the comments the size was not mentioned thus downloading wasn't successful but things worked when in declared encoding as binary mentioned the size and changed the zip name or deleted the previous zip file.

ZipArchive invalid in Windows only

After inspecting the ZIP files, there's HTML coming after the ZIP content. The fix is to make sure to call exit as soon as possible after calling readfile so that nothing else is written to the stream.



Related Topics



Leave a reply



Submit