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

747 views Asked by At

Getting error file corrupt error when downloading large files using Ionic.zip in c#. 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. I am creating zip file of existing pdf files which are added in one zip files. There may be 5 files or may 1000 files of minimum 10 MB each.

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.

1

There are 1 answers

0
Evik Ghazarian On

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. Try switching to x64 temporarily and that might fix your problem.

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:

An article about LOH