简体   繁体   English

NET 6:无法在 Windows 中打开 ZipArchive

[英]NET 6: ZipArchive can't be opened in Windows

I'm trying to write a process that minify's images.我正在尝试编写一个缩小图像的过程。 The process accepts single images, or a zip file.该过程接受单个图像或 zip 文件。

If it's a zip file, it extracts the zip and minify's valid files.如果它是一个 zip 文件,它会提取 zip 和 minify 的有效文件。

The process returns a zip file.该过程返回一个 zip 文件。 However, when opening in windows, it throws an error that the zip can't be opened.但是在windows打开的时候报zip打不开的错误。 If I open in 7zip, it works fine.如果我在 7zip 中打开,它工作正常。

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);

This was solved by a previous post: Building a corrupted zip file using ASP.Net core and Angular 6上一篇文章解决了这个问题: 使用 ASP.Net 核心构建损坏的 zip 文件和 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM