title: Export and compress CSV .Net Core
body: <p>I need an API that's export CSV File as ZIP From my database using .Net Core APIs
  so i Follow this way :-</p>
  <ol>
  <li>Export CSV file to <strong>temp folder</strong> using <strong>CSV Helper Library</strong></li>
  <li>Compress it</li>
  <li>Delete this CSV from the <strong>temp folder</strong></li>
  </ol>
  <p>and this's the code i am using</p>
  <pre><code>    [HttpGet(&quot;exportAsZip&quot;)]
      public FileResult DownloadZipFileasas()
      {
          //Create temp CSV file
          var cc = new CsvConfiguration(new System.Globalization.CultureInfo(&quot;en-US&quot;));
          using (MemoryStream ms = new MemoryStream())
          {
              var query = _context.AbdaInaktivProtokoll.Take(50).ToList();
              using (var sw = new StreamWriter(stream: ms, encoding: new UTF8Encoding(true)))
              {
                  using (var cw = new CsvWriter(sw, cc))
                  {
                      cw.WriteRecordsAsync(query);
                  }
                  var tempData = File(ms.ToArray(), &quot;text/csv&quot;, &quot;data.csv&quot;);
                  //System.IO.File.Copy(@&quot;C:\Temp\data.csv&quot;, @&quot;C:\Temp\data.csv&quot;);
                  //string fileName = Path.Combine(@&quot;C:\Temp\data.csv&quot;, &quot;data.csv&quot;);
              }
  
              //Compressing CSV file
              using (ZipArchive arch = new ZipArchive(ms, ZipArchiveMode.Create, true))
              {
                  arch.CreateEntryFromFile(@&quot;C:\Temp\data.csv&quot;, &quot;data.csv&quot;);
              }
              MemoryStream dolly = new MemoryStream(ms.ToArray());
              using (FileStream file = new FileStream(@&quot;C:\Temp\data.csv&quot;, FileMode.OpenOrCreate, FileAccess.ReadWrite))
              {
                  byte[] bytes = new byte[dolly.Length];
                  dolly.ReadAsync(bytes, 0, (int)dolly.Length);
                  file.WriteAsync(bytes, 0, bytes.Length);
                  file.FlushAsync();
                  dolly.Close();
  
                  //Delete Csv temp file after it compressed
                  file.DisposeAsync();
                  System.IO.File.Delete(@&quot;C:\Temp\data.csv&quot;);
  
                  Response.Headers.Add(&quot;Content-Disposition&quot;, &quot;attachment; filename=Dateien Exportieren_&quot; + (DateTime.UtcNow.ToShortDateString()) + &quot;.zip&quot;);
                  return File(bytes, &quot;application/zip&quot;);
  
              }
  
          }
      }
  </code></pre>
  <p>and it keeps show me this error</p>
  <pre><code>System.ObjectDisposedException: Cannot access a closed Stream.
  </code></pre>