繁体   English   中英

如何在 c# 中使用 Ionic-zip 下载大文件时修复 zip 文件损坏错误

[英]How to fix zip file corrupt error while downloading large files with Ionic-zip in c#

使用 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 中下载大文件。

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

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

我还建议对 LOH(大型 Object 堆)进行研究,知道它会在您将来处理这些情况时派上用场:

关于 LOH 的文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM