【问题标题】:Issues downloading zip files ASP MVC下载 zip 文件 ASP MVC 的问题
【发布时间】:2016-07-12 19:02:20
【问题描述】:

我正在使用一个将一些文件压缩在一起的 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 无法打开该文件夹。压缩(压缩)文件夹无效。

【问题讨论】:

  • 你在使用哪个库ZipFile
  • DotNetZip。但是,我刚刚尝试使用 System.IO.Compression,但出现了另一个错误,即 zip 无效。
  • 您的GetFileList 是什么样的?我找到了这个support.microsoft.com/en-us/kb/952249。你的GetFileList() 也是返回目录吗?

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

您使用的是什么版本的 DotNetZip?最近的版本有一个错误。 尝试安装旧版本(1.9)并运行您的代码。如果有效,则该错误仍未修复。

以下是工作项的链接。 https://dotnetzip.codeplex.com/workitem/14087

【讨论】:

  • 这就是问题所在。我的很多文件最终都是 128 的倍数并出现此问题。我尝试设置 zip.ParallelDeflateThreshold = -1 并修复它。
【解决方案2】:

我使用以下代码进行了测试,没有发现任何问题。以下代码是安装最新 NuGet 包DotNetZip 后的完整工作示例。我不知道 GetFiles 做了什么,所以我用我从硬编码目录中提取的一些文件替换了它。我猜你的问题可能就在那里,所以也许你可以分享那个代码。同样,这是一个使用 MVC 控制器的测试,你已经说明了,我只改变了一个东西来表明没有问题

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Ionic.Zip;

namespace MvcTesting.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            // replace C:\PickAFolderWithSomeFiles\ with a folder that has some files that you can zip to test
            List<string> files = System.IO.Directory.GetFiles(@"C:\PickAFolderWithSomeFiles\").ToList();
            using (ZipFile zip = new ZipFile())
            {
                foreach (string file in files)
                    zip.AddFile(file, string.Empty);

                using (MemoryStream output = new MemoryStream())
                {
                    zip.Save(output);
                    return File(output.ToArray(), "application/zip", "file.zip");
                }
            }
        }
    }
}

【讨论】:

  • 感谢您为此付出的努力。当文件超过一定大小和 128 的倍数时,似乎是 DotNetZip 中的一个错误导致问题。
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2017-11-27
相关资源
最近更新 更多