0

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();
  • 1
    Do you have to do it in memory? – ProgrammingLlama Jan 27 '20 at 07:47
  • 1
    [Related question](https://stackoverflow.com/questions/15595061/outofmemoryexception-while-populating-memorystream-256mb-allocation-on-16gb-sys) – ProgrammingLlama Jan 27 '20 at 07:49
  • 1
    does the machine your running this on have enough memory... basically what john said, just ensure that you consider where this is running as well. – Seabizkit Jan 27 '20 at 08:05
  • @Seabizkit Yes machine has enough memory. Even in developer machine which has 16GB RAM and only 8.3GB used, same issue occurs. – Novice Programmer Jan 27 '20 at 08:44
  • 2
    visual studio, tend to default to 32 bit, ensure that your app is running in 64bit. debug the line, which is appending to the mem buffer to see how large it is. my guess is your in 32 bit but also your run into the issue which John gave a link for. my guess is you want to avoid the MemoryStream try something like https://stackoverflow.com/questions/22339260/how-do-i-add-files-to-an-existing-zip-archive – Seabizkit Jan 27 '20 at 08:57

0 Answers0