2

I am creating a zip file the code below

ZipArchive MyZipArchive = System.IO.Compression.ZipFile.Open(@"c:\ZipProject\Test.zip", ZipArchiveMode.Update);

I then add files to the zip as below. There are around 10 files in fielList with file size of 250 mb each

foreach (string f in fileList)
{ 
     MyZipArchive.CreateEntryFromFile(f,Path.GetFileName(f),CompressionLevel.Fastest );
}

On the very first file I get OutOfMeomry Exception Is there anyway around adding large files to a zip? I have tried all compressions but the result is the same

user2837961
  • 1,505
  • 3
  • 27
  • 67
  • Are you using a 32 bit build configuration? What if you use x64? – Crowcoder Sep 05 '18 at 10:42
  • I am using 32. Is there no option with 32? It would be difficult to upgrade at this point – user2837961 Sep 05 '18 at 10:45
  • I upgraded it to 64 bit but unfortunately the error is still there – user2837961 Sep 05 '18 at 10:50
  • 32 bit can address 10 x 250mb but it is available *contiguous* memory that your process seems to be unable to find. I never recommend doing this, but what if you GC.Collect() before adding each file? – Crowcoder Sep 05 '18 at 10:50
  • you could create a quick project with only the compression logic and check if you reproduce the problem in 32 bit, if so changing it to x64 would be easy to test and narrow down the cause of the problem. Anyway, it is unlikely to be solved if you have in memory the whole file due to [32 bit procceses memory limit](https://en.wikipedia.org/wiki/2_GB_limit). You may try a different approach, using streams. – Cleptus Sep 05 '18 at 10:51

2 Answers2

1

Test your code with a few files of only a few MB each and an empty zip file. If that fails, the issue is not about memory, but a programming error. If it works, you are actually low on virtual memory. In that case, you you will have to switch to x64 in order to address more than 4 GB of memory. That is assuming you have sufficient physical memory.

user82593
  • 170
  • 8
1

I tested your example (full code bellow)

using System;
using System.IO;
using System.IO.Compression;

namespace ZipTest
{
  internal static class Program
  {
    private static void Main()
    {
      var fileList = new[] { @"d:\Test\file1", @"d:\Test\file2", @"d:\Test\file3", @"d:\Test\file4",
        @"d:\Test\file5", @"d:\Test\file6", @"d:\Test\file7", @"d:\Test\file8", @"d:\Test\file9", @"d:\Test\file10" };
      using (var MyZipArchive = ZipFile.Open(@"d:\Test\Test.zip", ZipArchiveMode.Update))
      {
        foreach (var f in fileList)
        {
          Console.WriteLine("Compressing: " + f);
          MyZipArchive.CreateEntryFromFile(f, Path.GetFileName(f), CompressionLevel.Fastest);
        }
      }
    }
  }
}

Total size of files approximately 2 GB, compressed size almost the same (I used installers, since I did not find any other large files).

First run caused out of memory exception (after the last file). This seems to have caused Windows to clear (free unused) memory and no other run caused exception. Total virtual memory of the application was approximately 3.5 GB

See the duplicates in comments. This might help you.

Julo
  • 1,102
  • 1
  • 11
  • 19
  • Only as a note: change to `ZipArchiveMode.Create` caused, that the virtual memory size was less than 150 MB; it seems like in update mode everything is stored in RAM. – Julo Sep 05 '18 at 12:40