我正在 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 方法从 Angular 脚本调用,并转换为链接到元素的客户端 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:\Users\Mark\AppData\Local\Temp\iisexpress\IIS Temporary Compressed Files\Clr4IntegratedAppPool is invalid. Static compression is being disabled. Event Xml:
2264 3 0 0x80000000000000 90672 Application Muddlehead C:\Users\Mark\AppData\Local\Temp\iisexpress\IIS Temporary Compressed Files\Clr4IntegratedAppPool 03000000
关于如何解决这个问题的想法?
最佳答案
使用 ZipArchive 时应该写“using”。 当 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");
这样写时你会得到错误的 zipper
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");
关于asp.net-web-api - MVC 核心 ZipArchive 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970129/