加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-web-api – MVC Core ZipArchive无效

发布时间:2020-12-16 03:50:01 所属栏目:asp.Net 来源:网络整理
导读:我正在MVC Core下的Web API调用中创建一个zip文件,但 Windows无法打开生成的文件,声称它无效. 以下是创建存档的代码: ZipArchive archive = new ZipArchive( archiveMS,ZipArchiveMode.Create,true );// loop over a series of Azure blobs which contain t
我正在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" );

从角度脚本调用此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 );

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

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

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

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

Log Name: Application Source: IIS Express Date:
1/31/2017 4:20:15 PM Event ID: 2264 Task Category: None Level:
Warning Keywords: Classic User: N/A Computer:
Muddlehead Description: The directory specified for caching compressed
content C:UsersMarkAppDataLocalTempiisexpressIIS Temporary
Compressed FilesClr4IntegratedAppPool is invalid. Static compression
is being disabled. Event Xml:

2264
3
0
0x80000000000000

90672
Application
Muddlehead

C:UsersMarkAppDataLocalTempiisexpressIIS Temporary Compressed FilesClr4IntegratedAppPool
03000000

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

解决方法

在使用ZipArchive时,您应该写’使用’.
当IDisposible运行时,它正确完成您的文件.

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

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

这样写的时候会得到坏的zip

byte [] zipBytes;
using (MemoryStream ms = new MemoryStream())
{
    var file1 = Encoding.ASCII.GetBytes("Hello,CompressionLevel.Fastest);
        using (var zipStream = zipArchiveEntry.Open()) 
        {
           zipStream.Write(file1,file1.Length);
        }
        zipBytes = ms.ToArray(); //bad place to assign
    }

}
return File(zipBytes,"Archive.zip");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读