title: Create ZipArchive of Excel XLSX file with EPPLUS C#
body: <p>I have the folowing function to return a <code>FileStreamResult</code> for my ASP.NET MVC application.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Generates a FileStreamResult containing a zip file with the EXCEL file in it
  /// &lt;/summary&gt;
  /// &lt;typeparam name="T"&gt;Type of object in the object list parameter&lt;/typeparam&gt;
  /// &lt;param name="objectList"&gt;The object list enumerable. This contains the data for the EXCEL file&lt;/param&gt;
  /// &lt;param name="fileName"&gt;The file name of the EXCEL&lt;/param&gt;
  /// &lt;returns&gt;FileStreamResult&lt;/returns&gt;
  public FileStreamResult CreateZipFileFileStreamResult&lt;T&gt;(IEnumerable&lt;T&gt; objectList, string fileName)
  {   
      var ms = new MemoryStream();
  
      var contentType = System.Net.Mime.MediaTypeNames.Application.Zip;
  
      ExcelPackage excelPackage = null;
  
      ZipArchive archive = null;
  
      try
      {
          excelPackage = new ExcelPackage(ms);
  
          var workSheet1 = excelPackage.Workbook.Worksheets.Add("Sheet1");
  
          workSheet1.Cells["A1"].LoadFromCollection&lt;T&gt;(objectList, true);
  
          excelPackage.SaveAs(ms);
  
  
  
          ms.Seek(0, SeekOrigin.Begin);
  
          archive = new ZipArchive(excelPackage.Stream, ZipArchiveMode.Create, true);
  
          var newEntry = archive.CreateEntry(fileName, System.IO.Compression.CompressionLevel.Fastest);
  
          var newEntryStream = newEntry.Open();
  
          var fsr = new FileStreamResult(excelPackage.Stream, contentType);
          fsr.FileDownloadName = fileName + ".zip";
  
          return fsr;
      }
      catch (Exception ex)
      {
          if (archive != null)
              archive.Dispose();
  
          if (excelPackage != null)
              excelPackage.Dispose();
  
          if (ms != null)
              ms.Dispose();
  
          throw;
      }
  }
  </code></pre>
  
  <p>The function returns something, but is is in a split XML fashion rather that one XLSX file.</p>
  
  <p>I want it to a return a ZIPPED single file.</p>
  
  <p>This is what the current result looks like.</p>
  
  <p><a href="https://i.stack.imgur.com/E0O37.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E0O37.png" alt="ZIP archive structure"></a></p>
  
  <p>Having used the help given by @kuujinbo I have created this function.
  Please note that for some reason FileContentResult works and FileStreamResult does not work.</p>
  
  <pre><code>        /// &lt;summary&gt;
          /// Generates a FileStreamResult containing a zip file with the EXCEL file in it
          /// &lt;/summary&gt;
          /// &lt;typeparam name="T"&gt;Type of object in the object list parameter&lt;/typeparam&gt;
          /// &lt;param name="objectList"&gt;The object list enumerable. This contains the data for the EXCEL file&lt;/param&gt;
          /// &lt;param name="fileName"&gt;The file name of the EXCEL&lt;/param&gt;
          /// &lt;returns&gt;FileStreamResult&lt;/returns&gt;        
          public FileContentResult CreateZipFileFileContentResult&lt;T&gt;(IEnumerable&lt;T&gt; objectList, string fileName)
          {
              var contentType = System.Net.Mime.MediaTypeNames.Application.Zip;
  
              using (var memoryStream = new System.IO.MemoryStream())
              {
                  using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                  {
                      using (var package = new OfficeOpenXml.ExcelPackage())
                      {
                          var workSheet1 = package.Workbook.Worksheets.Add("Sheet1");
                          workSheet1.Cells["A1"].LoadFromCollection&lt;T&gt;(objectList, true);
  
                          var firstRow = workSheet1.Row(1);
                          if (firstRow != null)
                              firstRow.Style.Font.Bold = true;
  
                          zip.AddEntry(fileName, package.GetAsByteArray());
                          zip.Save(memoryStream);
                          var fcr = new FileContentResult(memoryStream.ToArray(), contentType); //NOTE: Using a File Stream Result will not work.
                          fcr.FileDownloadName = fileName + ".zip";
                          return fcr;
                      }
                  }
              }
          }
  </code></pre>