NET 6: ZipArchive can't be opened in Windows

346 Views Asked by At

I'm trying to write a process that minify's images. The process accepts single images, or a zip file.

If it's a zip file, it extracts the zip and minify's valid files.

The process returns a zip file. However, when opening in windows, it throws an error that the zip can't be opened. If I open in 7zip, it works fine.

Has anyone run into this problem before and have any suggestions:

MemoryStream outputStream = new MemoryStream();
if (ImageMinificationHelpers.ZipTypes.Contains(file.ContentType))
{
    ZipArchive compressedArchive = new ZipArchive(outputStream, ZipArchiveMode.Create);
    using (ZipArchive archive = new ZipArchive(file.OpenReadStream()))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            if (ImageMinificationHelpers.ValidFileExtensions.Contains(Path.GetExtension(entry.FullName)))
            {
                ZipArchiveEntry zipArchiveEntry = compressedArchive.CreateEntry(entry.Name);
                using (Stream zipStream = zipArchiveEntry.Open())
                {
                    MemoryStream unzippedEntryStream = await ImageMinificationHelpers.Compress(entry.Open(), width, height, quality, cancellationToken);
                    await unzippedEntryStream.CopyToAsync(zipStream, cancellationToken);
                }
             }
         }
     }
 }
 else
 {
     outputStream = await ImageMinificationHelpers.Compress(file.OpenReadStream(), width, height, quality, cancellationToken);
 }
 outputStream.Seek(0, SeekOrigin.Begin);
 return File(outputStream, "application/zip", file.FileName);
1

There are 1 best solutions below

0
On BEST ANSWER

This was solved by a previous post: Building a corrupted zip file using ASP.Net core and Angular 6

MemoryStream outputStream = new();
if (ImageMinificationHelpers.ZipTypes.Contains(file.ContentType))
{
    /* 
      This is the important difference. 
      Put it in a using, and set Keep Open as true. 
      This ensures the memory stream doesn't get disposed.
    */
    using (ZipArchive compressedArchive = new(outputStream, ZipArchiveMode.Create, true))
    {
        using (ZipArchive archive = new(file.OpenReadStream()))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                if (ImageMinificationHelpers.ValidFileExtensions.Contains(Path.GetExtension(entry.FullName)))
                {
                    ZipArchiveEntry zipArchiveEntry = compressedArchive.CreateEntry(entry.Name);
                    using (Stream zipStream = zipArchiveEntry.Open())
                    {
                        MemoryStream unzippedEntryStream = await ImageMinificationHelpers.Compress(entry.Open(), width, height, quality, cancellationToken);
                        await unzippedEntryStream.CopyToAsync(zipStream, cancellationToken);
                    }
                }
            }
        }
    }
}
else
{
    outputStream = await ImageMinificationHelpers.Compress(file.OpenReadStream(), width, height, quality, cancellationToken);
}
outputStream.Seek(0, SeekOrigin.Begin);
return File(outputStream, file.ContentType, file.FileName);