【发布时间】:2018-05-22 07:35:04
【问题描述】:
我正在尝试使用一些字节数组数据动态创建一个 zip 流,并通过我的 MVC 操作将其下载。
但是下载的文件在windows中打开时总是报以下损坏的错误。
当我尝试从 7z 提取时出现此错误
但请注意,从 7z 中提取的文件没有损坏。
我正在使用ZipArchive,下面是我的代码。
private byte[] GetZippedPods(IEnumerable<POD> pods, long consignmentID)
{
using (var zipStream = new MemoryStream())
{
//Create an archive and store the stream in memory.
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");
}
我在这里做错了什么。
任何帮助都将不胜感激,因为我为此苦苦挣扎了一整天。
提前致谢
【问题讨论】:
标签: c# .net asp.net-mvc system.io.compression