我试图从SQL数据库中存储为字节数组的文件生成zip文件。我正在使用以下代码:
public async Task<IActionResult> Download(int id)
{
var files = this.assignmentsService.GetFilesForAssignment(id).ToList();
using var memoryStream = new MemoryStream();
using var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
foreach (var file in files)
{
var zipFile = archive.CreateEntry(file.Name);
using var streamWriter = new StreamWriter(zipFile.Open());
streamWriter.Write(file.Content);
}
return this.File(memoryStream.ToArray(), "application/zip", "Description.zip");
}
当我运行它时,文件被成功下载,所有文件都在存档中。但当我尝试打开其中一个时,会出现以下错误:
C:\Users\User\Downloads\Description。zip:存档意外结束
您应该处理ZipArchive
,以确保它完成对目标流的写入。您可以通过将ZipArchive
包装在using块中,而不是在此处使用using声明来实现这一点:
using var memoryStream = new MemoryStream();
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
var zipFile = archive.CreateEntry(file.Name);
using var streamWriter = new StreamWriter(zipFile.Open());
streamWriter.Write(file.Content);
}
}
return this.File(memoryStream.ToArray(), "application/zip", "Description.zip");
注意,文件
方法也有一个on重载,它直接获取流。这样,您就可以在保持内存流打开的同时使用ZipArchive的using声明(在将流写入HTTP响应后,File()
调用将最终处理该流):
var memoryStream = new MemoryStream();
using var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
foreach (var file in files)
{
var zipFile = archive.CreateEntry(file.Name);
using var streamWriter = new StreamWriter(zipFile.Open());
streamWriter.Write(file.Content);
}
return File(memoryStream, "application/zip", "Description.zip");