title: Compress large files using .NET Framework ZipArchive class on ASP.NET
body: <p>I have a code that get all files on a directory, compresses each one and creates a .zip file. I'm using the .NET Framework ZipArchive class on the System.IO.Compression namespace and the extension method CreateEntryFromFile. This is working well except when processing large files (aproximately 1GB and up), there it throws a <code>System.IO.Stream Exception "Stream too large"</code>.</p>
  
  <p>On the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfileextensions.createentryfromfile?view=netframework-4.7.2#System_IO_Compression_ZipFileExtensions_CreateEntryFromFile_System_IO_Compression_ZipArchive_System_String_System_String_" rel="nofollow noreferrer">extension method reference on MSDN</a> it states that: </p>
  
  <blockquote>
    <p>When ZipArchiveMode.Update is present, the size limit of an entry is limited to Int32.MaxValue. This limit is because update mode uses a MemoryStream internally to allow the seeking required when updating an archive, and MemoryStream has a maximum equal to the size of an int.</p>
  </blockquote>
  
  <p>So this explains the exception I get, but provides no further way of how to overcome this limitation. How can I allow large file proccesing?</p>
  
  <p>Here is my code, its part of a class, just in case, the <code>GetDatabaseBackupFiles()</code> and  <code>GetDatabaseCompressedBackupFiles()</code> functions returns a list of FileInfo objects that I iterate:</p>
  
  <pre><code>public void CompressBackupFiles()
  {
      var originalFiles = GetDatabaseBackupFiles();
      var compressedFiles = GetDatabaseCompressedBackupFiles();
      var pendingFiles = originalFiles.Where(c =&gt; compressedFiles.All(d =&gt; Path.GetFileName(d.Name) != Path.GetFileName(c.Name)));
      foreach (var file in pendingFiles)
      {
          var zipPath = Path.Combine(_options.ZippedBackupFilesBasePath, Path.GetFileNameWithoutExtension(file.Name) + ".zip");
          using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
          {
               archive.CreateEntryFromFile(file.FullName, Path.GetFileName(file.Name));
          }
      }
      DeleteFiles(originalFiles);
  }
  </code></pre>