【发布时间】:2020-08-30 15:28:43
【问题描述】:
我正在尝试在内存中创建一个 Zip 文件,添加一些其他文件作为存档的条目,并使用以下命令从我的 Web API 控制器返回它:
try {
// Create Zip Archive in Memory
MemoryStream memoryStream = new MemoryStream();
ZipArchive zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create);
foreach(Worksheet w in worksheets) {
string fullPath = Path.Combine(baseFolder, w.FilePathAndName);
if(System.IO.File.Exists(fullPath)) {
ZipArchiveEntry f = zipArchive.CreateEntry(w.FileName, CompressionLevel.Fastest);
using(Stream entryStream = f.Open()) {
FileStream fileStream = System.IO.File.OpenRead(fullPath);
await fileStream.CopyToAsync(entryStream);
}
}
}
memoryStream.Seek(0, SeekOrigin.Begin);
return File(memoryStream, "application/zip", $"Files.zip");
} catch(Exception e) {
return StatusCode(500, "Error: Could not generate zip file");
}
这确实会创建并返回一个 zip 文件,但是它是无效的。 7-Zip gives me this error
运行unzip -t Files.zip 测试存档结果如下:
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
note: Files.zip may be a plain executable, not an archive
unzip: cannot find zipfile directory in one of Files.zip or
Files.zip.zip, and cannot find Files.zip.ZIP, period.
.NET Core 版本 3.1.201
【问题讨论】:
标签: .net .net-core asp.net-core-webapi zipfile memorystream