我正在尝试在内存中创建一个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给出了如下错误提示
运行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 版本
Seek(...)
调用到using (var archive = new ZipArchive(...))
块外是我之前遗漏的,这正是同样的问题。 - TheMSGmemoryStream.Seek([...])
,否则它就无法工作。 - Byte