繁体   English   中英

使用多个文件创建Zip文件C#

[英]Create Zip File with multiple files C#

我正在使用从互联网下载的字节数[]创建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");
}

移动

zipBytes = compressedFileStream.ToArray();

处置完归档文件后执行“ 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");
}

参考ZipArchive.Dispose()

此方法完成了归档文件的编写,并释放了ZipArchive对象使用的所有资源。 除非您使用ZipArchive(Stream, ZipArchiveMode, Boolean)构造函数重载来构造对象,否则将其leaveOpen参数设置为trueleaveOpen所有基础流都将关闭,并且不再可用于后续的写操作。

完成使用此ZipArchive实例后,调用Dispose()释放该实例使用的所有资源。 您应该消除对该ZipArchive实例的进一步引用,以便垃圾收集器可以回收该实例的内存,而不是保留该内存以进行最终化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM