title: Calculate size of zip file with compression level 0
body: <p>I am writing functionality for our web server which should download several files from other servers, and return them as a zip archive without compression.</p>
  
  <p>How can I determine the final size of the ZIP archive if I know the sizes of all downloaded files?</p>
  
  <p>This is the code which I am working on for the moment. The commented line caused corruption of the ZIP archive.</p>
  
  <pre><code>public void Download()
  {
      var urls = Request.Headers["URLS"].Split(';');
      Task&lt;WebResponse&gt;[] responseTasks = urls
          .Select(it =&gt;
          {
              var request = WebRequest.Create(it);
              return Task.Factory.FromAsync&lt;WebResponse&gt;(request.BeginGetResponse(null, null), request.EndGetResponse);
          })
          .ToArray();
  
      Task.WaitAll(responseTasks);
  
      var webResponses = responseTasks.Where(it =&gt; it.Exception == null).Select(it =&gt; it.Result);
  
      var totalSize = webResponses.Sum(it =&gt; it.ContentLength + 32);
  
      Response.ContentType = "application/zip";
      Response.CacheControl = "Private";
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      // Response.AddHeader("Content-Length", totalSize.ToString(CultureInfo.InvariantCulture));
  
      var sortedResponses = webResponses.OrderBy(it =&gt; it.ContentLength);
  
      var buffer = new byte[32 * 1024];
  
      using (var zipOutput = new ZipOutputStream(Response.OutputStream))
      {
          zipOutput.SetLevel(0);
  
          foreach (var response in sortedResponses)
          {
              var dataStream = response.GetResponseStream();
  
              var ze = new ZipEntry(Guid.NewGuid().ToString() + ".jpg");
              zipOutput.PutNextEntry(ze);
  
              int read;
              while ((read = dataStream.Read(buffer, 0, buffer.Length)) &gt; 0)
              {
                  zipOutput.Write(buffer, 0, read);
                  Response.Flush();
              }
  
              if (!Response.IsClientConnected)
              {
                  break;
              }
          }
  
          zipOutput.Finish();
      }
  
      Response.Flush();
      Response.End();
  }
  </code></pre>