MVC Core ZipArchive无效

Mar*_*ert 6 asp.net-web-api asp.net-core-mvc asp.net-core

我正在MVC Core下的Web API调用中创建一个zip文件,但Windows无法打开生成的文件,声称它无效.

以下是创建存档的代码:

ZipArchive archive = new ZipArchive( archiveMS, ZipArchiveMode.Create, true );

// loop over a series of Azure blobs which contain text
foreach( BlobPathInfo curBPI in model.Paths )
{
    // AzureBlobFile is a wrapper for CloudBlockBlob
    AzureBlobFile blobFile = blobFolder.File( curBPI.BlobPath.FileName );
    Stream blobStream = blobFile.OpenRead();

    ZipArchiveEntry entry = archive.CreateEntry( zipFolderPath.ToString() );

    using( Stream zipStream = entry.Open() )
    {
        blobStream.CopyTo( zipStream );
    }
}

archive.Dispose();
archiveMS.Seek( 0, SeekOrigin.Begin );

return new FileStreamResult( archiveMS, "application/zip" );
Run Code Online (Sandbox Code Playgroud)

从角度脚本调用此WebAPI方法,并将其转换为链接到元素的客户端blob:

// downloadFiles does a POST request and returns a promise
downloadFiles( params )
.then( function( success ) {
    var linkElement = document.createElement( 'a' );
    var blob = new Blob( [success.data], { type: 'application/zip' } );
    var url = window.URL.createObjectURL( blob );

    linkElement.setAttribute( 'href', url );
    linkElement.setAttribute( 'download', fileName );

    var clickEvent = new MouseEvent( 'click',
    {
        view: window,
        bubbles: true,
        cancelable: false
    } );

    linkElement.dispatchEvent( clickEvent );
Run Code Online (Sandbox Code Playgroud)

这一切都在于,存档在服务器上创建,下载到客户端,然后通过文件保存对话框保存.但就Windows而言,磁盘上生成的存档无效.

Windows错误消息没有帮助,基本上只是声明该文件无效.但我确实注意到另外两件可能很重要的事情:

1)如果我没有在服务器上的zip存档中创建一个条目 - 如果我只是创建存档并下载它 - 它在Windows下正确打开(当然,它表明它没有内容/条目).

2)在检查错误日志时,我注意到以下情况:

日志名称:应用程序源:IIS Express日期:
1/31/2017 4:20:15 PM事件ID:2264任务类别:无级别:
警告关键字:经典用户:N/A计算机:
Muddlehead说明:指定用于缓存的目录压缩内容C:\ Users\Mark\AppData\Local\Temp\iisexpress\IIS临时压缩文件\ Clr4IntegratedAppPool无效.静态压缩正在被禁用.事件Xml:
2264 3 0 0x80000000000000 90672应用程序Muddlehead C:\ Users\Mark\AppData\Local\Temp\iisexpress\IIS临时压缩文件\ Clr4IntegratedAppPool 03000000

关于如何解决这个问题的想法?

小智 5

使用 ZipArchive 时,您应该编写“使用”。当 IDisposible 运行时,它会正确完成您的文件。

byte [] zipBytes;
using (MemoryStream ms = new MemoryStream())
{
    var file1 = Encoding.ASCII.GetBytes("Hello,world!");
    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
    {
        var zipArchiveEntry = archive.CreateEntry("file1.txt",     CompressionLevel.Fastest);
        using (var zipStream = zipArchiveEntry.Open()) 
          { zipStream.Write(file1, 0, file1.Length); }

    }
    zipBytes = ms.ToArray(); // good place to assign
}
return File(zipBytes, "application/zip", "Archive.zip");
Run Code Online (Sandbox Code Playgroud)

像这样写时,你会得到不好的 zip

byte [] zipBytes;
using (MemoryStream ms = new MemoryStream())
{
    var file1 = Encoding.ASCII.GetBytes("Hello,world!");
    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
    {
        var zipArchiveEntry = archive.CreateEntry("file1.txt",     CompressionLevel.Fastest);
        using (var zipStream = zipArchiveEntry.Open()) 
        {
           zipStream.Write(file1, 0, file1.Length);
        }
        zipBytes = ms.ToArray(); //bad place to assign
    }

}
return File(zipBytes, "application/zip", "Archive.zip");
Run Code Online (Sandbox Code Playgroud)