handle files with large size

Mar 11, 2010 at 10:00 AM

hi, experts! I am a newbie to handle the files in C#.

I use this library v1.9 on asp.net

        protected void btnDownload2_Click(object sender, EventArgs e)


            System.Web.HttpContext c = System.Web.HttpContext.Current;

            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=" + "download.zip");

            ZipFile zip = new ZipFile("download.zip");

            FileStream fs = null;
            byte[] data = null;
            int length = 0;

            for (int i = 0; i <= 9; i++)
                fs = new FileStream("C:\\test_dotnetzip_files\\" + i.ToString() + ".bak", FileMode.Open);
                length = Convert.ToInt32(fs.Length);
                data = new byte[length];
                fs.Read(data, 0, length);

                zip.AddEntry("a\\" + i.ToString() + ".bak", data);    


in the above code, I use filestream and byte[] to add entry to the zipfile.
I found that byte[] uses memory, 
when the size of files are too large, it is so easy to make OutOfMemoryException occur.
how to handle this case when using DotNetZip?
Thank you very much!
Mar 11, 2010 at 12:40 PM

You are reading the entire contents of ALL of the files into memory, before putting them into the zip file.  Why not use the AddFile method?

for (int i = 0; i <= 9; i++)
    var filename = "C:\\test_dotnetzip_files\\" + i.ToString() + ".bak";
    zip.AddFile(filename, "a");    

Mar 11, 2010 at 12:54 PM

Dear Cheeso,

Hi, actually the files are store in the sql server as varbinary (data type).

the file are loaded and stored as byte[] at code behind.

are there any better ways?

Thank you very much!


Mar 11, 2010 at 12:58 PM

Yes, use AddEntry, with a WriteDelegate.  Inside your WriteDelegate, open a stream to the Sql Server and write it, in chunks (not all at once) to the zip archive.  Or, use the overload of AddEntry() that accepts an OpenDelegate and CloseDelegate. For that one, you need only to open the  stream in the opener, and close the stream in the closer, and DotNetZip writes the content from the stream in chunks into the zip file. 

The documentation has examples of these.


Mar 11, 2010 at 1:13 PM

the file size of each file may be 100 MB.

And there will be several big files to archive as one .zip file.

Thank you very much! let me test!