【问题标题】:Create Zip File with multiple files C#创建包含多个文件的 Zip 文件 C#
【发布时间】: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


【解决方案1】:

移动

zipBytes = compressedFileStream.ToArray();

在归档文件被释放之后,所有数据都被刷新到底层流中。

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 参数设置为true,否则所有底层流都将关闭并且不再可用于后续写入操作。

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

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2017-02-07
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多