ZipArchive给出了"Unexpected end of data corrupted"错误。

10 浏览
0 Comments

ZipArchive给出了"Unexpected end of data corrupted"错误。

我试图使用一些字节数组数据即时创建一个zip流,并通过我的MVC动作进行下载。但是在Windows中打开时,下载的文件总是出现以下损坏错误。

[图片链接](https://i.stack.imgur.com/UuQVC.png)

而且在尝试从7z中提取时出现以下错误。

[图片链接](https://i.stack.imgur.com/Ob3u4.png)

但请注意,从7z中提取的文件并没有损坏。

我正在使用ZipArchive,以下是我的代码:

private byte[] GetZippedPods(IEnumerable pods, long consignmentID)
{
    using (var zipStream = new MemoryStream())
    {
        //创建一个存储在内存中的存档。
        using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
        {
            int index = 1;
            foreach (var pod in pods)
            {                        
                var zipEntry = zipArchive.CreateEntry($"POD{consignmentID}{index++}.png", CompressionLevel.NoCompression);                       
                using (var originalFileStream = new MemoryStream(pod.ByteData))
                {
                    using (var zipEntryStream = zipEntry.Open())
                    {
                        originalFileStream.CopyTo(zipEntryStream);
                    }
                }
            }
            return zipStream.ToArray();
        }
    }
}
public ActionResult DownloadPOD(long consignmentID)
{
    var pods = _consignmentService.GetPODs(consignmentID);
    var fileBytes = GetZippedPods(pods, consignmentID);
    return File(fileBytes, MediaTypeNames.Application.Octet, $"PODS{consignmentID}.zip");
}

我在这里做错了什么。

非常感谢您的帮助,因为我已经为此挣扎了一整天。

提前致谢。

0