【发布时间】:2018-10-25 17:17:44
【问题描述】:
我正在从从 Internet 下载的文件中创建一个 Zip 文件,以字节 [] 为单位, 但我有一个问题,我不知道我做错了什么......我生成了 zip 文件,但它已损坏,文件大小正确(不是 0)。 请问你能帮帮我吗? 可能是我没看懂。
public <ActionResult> SomeFunction()
{
var invoices = GetInvoices();
WebClient client = new WebClient();
byte[] zipBytes = null;
using (var compressedFileStream = new MemoryStream())
{
using (ZipArchive zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create, leaveOpen: true))
{
foreach (var invoice in invoices)
{
// This has correct values.
byte[] fileBytes = client.DownloadData(invoice.XmlUri);
// Create the instance of the file.
var zipEntry = zipArchive.CreateEntry(invoice.XmlFileName);
// Get the stream of the file.
using (var entryStream = new MemoryStream(fileBytes))
// Get the Stream of the zipEntry
using (var zipEntryStream = zipEntry.Open())
{
// Adding the file to the zip file.
entryStream.CopyTo(zipEntryStream);
}
}
}
zipBytes = compressedFileStream.ToArray();
}
return File(zipBytes , System.Net.Mime.MediaTypeNames.Application.Octet, "test.zip");
}
【问题讨论】:
-
请尝试使用
zipEntryStream.Close();关闭zipEntryStream
标签: c# asp.net-mvc zip memorystream