简体   繁体   English

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

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

Getting error file corrupt error when downloading large files using Ionic.zip in c#.使用 c# 中的 Ionic.zip 下载大文件时出现错误文件损坏错误。 When user extracts that file file corrupt is being shown.当用户提取该文件时,将显示损坏的文件。 When i am doing same with smaller no of files than it is working fine.当我用更少的文件做同样的事情时,它工作正常。 Even it is not giving any error while creating zip files.即使在创建 zip 文件时也没有给出任何错误。 I am creating zip file of existing pdf files which are added in one zip files.我正在创建现有 pdf 文件的 zip 文件,这些文件添加到一个 zip 文件中。 There may be 5 files or may 1000 files of minimum 10 MB each.可能有 5 个文件或 1000 个文件,每个文件至少 10 MB。

I have tried with threshold and all other options provided on google, but didn't find any solutions.我尝试过使用谷歌提供的阈值和所有其他选项,但没有找到任何解决方案。

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();
}

I should be able to download large files inside zip.我应该能够在 zip 中下载大文件。

My explanation needs more space than a comment:我的解释需要比评论更多的空间:

On x32 platforms the usable ram limit for .Net is holding the program to compress files in larger sizes.在 x32 平台上, .Net的可用内存限制是让程序压缩更大尺寸的文件。 Try switching to x64 temporarily and that might fix your problem.尝试暂时切换到 x64,这可能会解决您的问题。

I also suggest research about LOH (Large Object Heap) which knowing it will come in handy for you in future dealing with these kind of scenarios:我还建议对 LOH(大型 Object 堆)进行研究,知道它会在您将来处理这些情况时派上用场:

An article about LOH 关于 LOH 的文章

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

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