title: how do I zip multiple files using GZipStream in c#
body: <p>I have created multiple csv files using datatable and want to zip all those file into one single zip file.
  Which is i'm doing all at dynamically.</p>
  
  <p>I tried following code</p>
  
  <pre><code>List&lt;string&gt; filestream = GenerateCSVfiles(dataSets);
  
  //Generate CSV files
  public List&lt;string&gt; GenerateCSVfiles(List&lt;DataSet&gt; dataSets)
  {
      List&lt;string&gt; filestream = new List&lt;string&gt;();
      StringBuilder result = null;
  
      foreach (DataSet dataSet in dataSets)
      {
          result = new StringBuilder();
  
          System.Data.DataTable dataTable = dataSet.Tables[0];
  
          foreach (DataColumn colm in dataTable.Columns)
          {
              result.Append(colm.ColumnName+",");
          }
  
          result.Append("\n");
  
          //create csv file
          foreach (DataRow row in dataTable.Rows)
          {
              for (int i = 0; i &lt; dataTable.Columns.Count; i++)
              {
                  result.Append(row[i].ToString());
                  result.Append(i == dataTable.Columns.Count - 1 ? "\n" : ",");
              }    
          }
  
          filestream.Add(result.ToString());   
      }
  
      return filestream;
  }
  </code></pre>
  
  <p>Now in <code>List&lt;filestream&gt;</code> I have all 2 file data want to create 2 different <code>.csv</code> files and create  one <code>temp.gz</code> file dynamically How can I do this ?</p>
  
  <p>On <code>ActionResult</code> method I tried following code to generate .zip file but it is corrupted</p>
  
  <pre><code>return File(CompressStringToFile("temp.gz", filestream), System.Net.Mime.MediaTypeNames.Application.Zip, "temp.gz");
  </code></pre>
  
  <p>// zip file generation code</p>
  
  <pre><code>public byte[] CompressStringToFile(string fileName, List&lt;string&gt; filestream)
  {   
      MemoryStream  f2 = new MemoryStream();
      FileStream fs = new FileStream();
      GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false);
  
      foreach (string oStr in filestream)
      {
          byte[] b = GetBytes(oStr);
          gz.Write(b, 0, b.Length);                    
      }
  
      return f2.ToArray();
  }
  </code></pre>
  
  <blockquote>
    <p>Correct me to generate .zip file. (with inside 2 .csv files)</p>
  </blockquote>