[英]Create Zip File with multiple files C#
I'm creating a Zip file from files I'm downloading from the internet in bytes[], but I have an Issue, I don't know what I'm doing wrong... I generate the zip file but it's corrupted, the file size is correct(is not 0). 我正在使用从互联网下载的字节数[]创建Zip文件,但出现问题,我不知道我做错了什么...我生成了zip文件,但文件已损坏,文件大小正确(不为0)。 Could you help me please?
请问你能帮帮我吗? Maybe I didn't understand it well.
也许我不太了解。
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");
}
Move 移动
zipBytes = compressedFileStream.ToArray();
To after the archive has been disposed so that all data is flushed to the underlying stream. 处置完归档文件后执行“ To”操作,以便将所有数据刷新到基础流。
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");
}
Reference ZipArchive.Dispose()
参考ZipArchive.Dispose()
This method finishes writing the archive and releases all resources used by the ZipArchive object.
此方法完成了归档文件的编写,并释放了ZipArchive对象使用的所有资源。 Unless you construct the object by using the
ZipArchive(Stream, ZipArchiveMode, Boolean)
constructor overload and set itsleaveOpen
parameter totrue
, all underlying streams are closed and no longer available for subsequent write operations.除非您使用
ZipArchive(Stream, ZipArchiveMode, Boolean)
构造函数重载来构造对象,否则将其leaveOpen
参数设置为true
,leaveOpen
所有基础流都将关闭,并且不再可用于后续的写操作。When you are finished using this instance of
ZipArchive
, callDispose()
to release all resources used by this instance.完成使用此
ZipArchive
实例后,调用Dispose()
释放该实例使用的所有资源。 You should eliminate further references to thisZipArchive
instance so that the garbage collector can reclaim the memory of the instance instead of keeping it alive for finalization.您应该消除对该
ZipArchive
实例的进一步引用,以便垃圾收集器可以回收该实例的内存,而不是保留该内存以进行最终化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.