我正在使用将某些文件压缩在一起的ASP MVC网站。它似乎正在工作,但是当我尝试提取文件时,我得到了
An unexpected error is keeping you from copying the file. If you continue the receive this error, you can use the error code to search for help with this problem.
enter code here
Error 0x80004005: Unspecified error
7 Zip表示CRC失败。文件已损坏。
似乎在编写或下载zip时东西已损坏。我尝试了2种不同的方法来下载具有相同结果的文件。
public ActionResult DownloadFiles()
{
List<string> files = GetFileList();
using (ZipFile zip = new ZipFile())
{
foreach (string file in files)
zip.AddFile(file, string.Empty);
MemoryStream output = new MemoryStream();
zip.Save(output);
return File(output.ToArray(), "application/zip", "file.zip");
}
}
public void DownloadFiles()
{
List<string> files = GetFileList();
using (ZipFile zip = new ZipFile())
{
foreach (string file in files)
zip.AddFile(file, string.Empty);
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=file.zip");
zip.Save(Response.OutputStream);
Response.End();
}
}
<button onclick="fn_DownloadFiles()">Download Selected</button>
function fn_DownloadSelectedFiles()
{
window.location.href = "/Files/DownloadFiles"
}
文件为什么会被损坏?
我也尝试过
public ActionResult DownloadFiles(string filePaths)
{
List<string> files = GetFileList();
using (MemoryStream zipStream = new MemoryStream())
using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
foreach (string file in files)
{
ZipArchiveEntry entry = zipArchive.CreateEntry(file, CompressionLevel.Fastest);
using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (Stream entryStream = entry.Open())
fileStream.CopyTo(entryStream);
}
return File(zipStream.ToArray(), "application/zip", "files.zip"));
}
}
这样我得到Windows无法打开该文件夹。压缩(压缩)的文件夹无效。
您正在使用哪个版本的DotNetZip?最新版本中存在一个错误。尝试安装旧版本(1.9)并运行您的代码。如果有效,则该错误仍未修复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句