1

在 c# 中使用 Ionic.zip 下载大文件时出现错误文件损坏错误。当用户提取该文件时,将显示损坏的文件。当我用更少的文件做同样的事情时,它工作正常。即使它在创建 zip 文件时也没有给出任何错误。我正在创建现有 pdf 文件的 zip 文件,这些文件添加到一个 zip 文件中。可能有 5 个文件或 1000 个文件,每个文件至少 10 MB。

我尝试过使用谷歌提供的阈值和所有其他选项,但没有找到任何解决方案。

using (ZipFile zip = new ZipFile())
{
    zip.AlternateEncodingUsage = ZipOption.AsNecessary;
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Level9;
    //zip.AddDirectoryByName("Files");

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        filePath = Convert.ToString(dt.Rows[i]["filePath"]);
        fileIds.Add(Convert.ToInt32(dt.Rows[i]["fileid"]));
        string fileFullName = Convert.ToString(dt.Rows[i]["fileName"]);
        string fName = Convert.ToString(dt.Rows[i]["fileName"]).Split('_')[1];

        //ExceptionLogging c1 = new ExceptionLogging("filePath = " + filePath + " fileFullName = " + fileFullName +
        //    " fName = " + fName);
        //c1.ErrorLog(HttpContext.Current.ApplicationInstance.Server.MapPath("~/Logs/"));


        if (!fileNamesList.Contains(fileFullName.Split('_')[0]))
        {
            //ExceptionLogging c2 = new ExceptionLogging("In if condition -- filePath = " + filePath);
            //c2.ErrorLog(HttpContext.Current.ApplicationInstance.Server.MapPath("~/Logs/"));

            zip.AddFile(filePath, "Files");
            fileNamesList.Add(fileFullName.Split('_')[0]);
        }
        else
        {
            filePath = filePath.Substring(0, filePath.Length - 4) + "_" + fName;
            //filePath = filePath.Split('.')[0] + "_" + fName;
            zip.AddFile(filePath, "Files");
            fileNamesList.Add(fileFullName);

            //ExceptionLogging c3 = new ExceptionLogging("In else condition -- filePath = " + filePath + " fileFullName = " + fileFullName);
            //c3.ErrorLog(HttpContext.Current.ApplicationInstance.Server.MapPath("~/Logs/"));
        }
        //zip.AddFile(filePath, "Files");
    }

    Response.Clear();
    Response.BufferOutput = false;
    string zipName = String.Format("ZipFile-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
    zip.Save(Response.OutputStream);
    //Response.End();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

我应该能够在 zip 中下载大文件。

4

1 回答 1

0

我的解释需要比评论更多的空间:

在 x32 平台上,可用 ram 限制.Net是保持程序以压缩更大尺寸的文件。尝试暂时切换到 x64,这可能会解决您的问题。

我还建议研究 LOH(大对象堆),知道它会在您将来处理这些场景时派上用场:

关于 LOH 的文章

于 2019-11-05T17:20:41.250 回答