【问题标题】:Archiving Multiple Files Using ZipOutputStream gives me an Empty Archived File使用 ZipOutputStream 归档多个文件会给我一个空的归档文件
【发布时间】:2015-03-24 20:28:35
【问题描述】:

我正在尝试通过 ZipoutputStream 下载我正在压缩(存档)的一堆文件。

using (var zipStream = new ZipOutputStream(outputMemStream))
{
    foreach (var documentIdString in documentUniqueIdentifiers)
    {   
        ...
        var blockBlob = container.GetBlockBlobReference(documentId.ToString());

        var fileMemoryStream = new MemoryStream();

        blockBlob.DownloadToStream(fileMemoryStream);

        zipStream.SetLevel(3); 

        fileMemoryStream.Position = 0;

        ZipEntry newEntry = new ZipEntry(document.FileName);
        newEntry.DateTime = DateTime.Now;

        zipStream.PutNextEntry(newEntry);

        fileMemoryStream.Seek(0, SeekOrigin.Begin);
        StreamUtils.Copy(fileMemoryStream, zipStream, new byte[4096]);

        zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.

    }

    outputMemStream.Seek(0, SeekOrigin.Begin);
    return outputMemStream;
}

在我的控制器中,我返回以下代码,该代码应下载我在上一个示例中创建的 Zip 文件。控制器操作在浏览器中下载文件,但存档文件为空。我可以看到从上述方法返回的填充内容长度...

file.Seek(0, SeekOrigin.Begin);
return File(file, "application/octet-stream", "Archive.zip");

有人知道为什么我的控制器返回的文件是空的或损坏的吗?

【问题讨论】:

    标签: c# .net memorystream sharpziplib


    【解决方案1】:

    我认为您需要关闭您的条目和最终的 zip 流。您还应该using 并处理所有流。试试这个:

    using (var zipStream = new ZipOutputStream(outputMemStream))
    {
        zipStream.IsStreamOwner = false;
        // Set compression level
        zipStream.SetLevel(3); 
    
        foreach (var documentIdString in documentUniqueIdentifiers)
        {   
            ...
            var blockBlob = container.GetBlockBlobReference(documentId.ToString());
    
            using (var fileMemoryStream = new MemoryStream())
            {
                // Populate stream with bytes
                blockBlob.DownloadToStream(fileMemoryStream);
    
                // Create zip entry and set date
                ZipEntry newEntry = new ZipEntry(document.FileName);
                newEntry.DateTime = DateTime.Now;
                // Put entry RECORD, not actual data
                zipStream.PutNextEntry(newEntry);
                // Copy date to zip RECORD
                StreamUtils.Copy(fileMemoryStream, zipStream, new byte[4096]);
                // Mark this RECORD closed in the zip
                zipStream.CloseEntry();
            }
        }
    
        // Close the zip stream, parent stays open due to !IsStreamOwner
        zipStream.Close();
    
        outputMemStream.Seek(0, SeekOrigin.Begin);
        return outputMemStream;
    }
    

    EDIT - 你应该删除:

    // Reset position of stream
    fileMemoryStream.Position = 0;
    

    很确定这是问题所在。

    【讨论】:

    • 所以我更新了我的代码,但是当我从控制器下载时,它仍然给我一个错误,它是无效的。即:压缩(压缩)文件夹“...”无效。
    • @jimfromthegym-JimMackin 它有尺寸吗?还是0字节?
    • @jimfromthegym-JimMackin 实际上不会重置流的位置。查看更新的代码。
    • 我将位置重置为 0 仍然无法正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多