Creating ZIP file larger than 150MB throws OutOfMemoryException C#

351 Views Asked by At

There is a client requirement to create a zip which consist of multiple files placed inside tree-like structured folders. It contains upto 150 files at maximum. When these files exceed approximately 160MB in memory stream OutOfMemoryException is thrown.

  1. List item
  2. Is there a config to increase memory allocated to this operation?
  3. Any other alternative ways to solve this?

Sample code

MemoryStream memStream = new MemoryStream();
using (var zipStream = new ZipOutputStream(memStream))
{
 foreach (FileModel fileToBeAddedInZip in listOfFiles)
 {
  byte[] fileBytes;
  fileBytes = //Read the file from DB

  ZipEntry fileEntry = null;
  fileEntry = new ZipEntry(fileToBeAddedInZip.fileName)
   {
      Size = fileBytes.Length
   };

  zipStream.PutNextEntry(fileEntry);
  zipStream.Write(fileBytes, 0, fileBytes.Length);
 }

}
zipStream.Flush();
zipStream.Close();
0

There are 0 best solutions below