title: Azure download blobs, create zip and send to user
body: <p>I'm using <a href="https://stackoverflow.com/a/20566524"><em>this</em></a> solution to download files, zip and send to user, (Azure blobs).</p>
  
  <p>This is what I have came up with so far:</p>
  
  <pre><code>    public void DownloadImages(DownloadImagesModel model)
      {
          if (ModelState.IsValid &amp;&amp; model.ImageIDs != null &amp;&amp; model.ImageIDs.Count &gt; 0)
          {
              var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
  
              var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
  
              if (images != null &amp;&amp; images.Count &gt; 0)
              {
                  string imageContainerURL = _fileService.GetImageContainerURL(currentUser.CompanyID) + currentUser.ImageContainerToken;
  
                  using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
                  {
                      zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
                      Response.BufferOutput = false;
                      Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.rar");
                      Response.ContentType = "application/octet-stream";
  
                      foreach (var image in images)
                      {
                          if (image.ImageLinks != null)
                          {
                              List&lt;ImageLinkModel&gt; imageLinks = new List&lt;ImageLinkModel&gt;();
  
                              if (model.FormatID == 0)
                                  imageLinks = image.ImageLinks.ToList();
                              else
                                  imageLinks = image.ImageLinks.Where(m =&gt; m.FormatID == model.FormatID).ToList();
  
                              if (imageLinks != null &amp;&amp; imageLinks.Count &gt; 0)
                              {
                                  //Every image has multiple blobs for different sizes
                                  foreach (var link in imageLinks)
                                  {
                                      CloudBlockBlob blob = _fileService.GetBlob(imageContainerURL, link.BlobName);
  
                                      var entry = new ZipEntry(image.ImageName)
                                      {
                                          DateTime = TimeZoneExtensions.GetCurrentDate(),
                                          Size = blob.Properties.Length
                                      };
  
                                      zipOutputStream.PutNextEntry(entry);
  
                                      //Download
                                      blob.DownloadToStream(zipOutputStream);
  
                                      Response.Flush();
  
                                      if (!Response.IsClientConnected)
                                      {
                                          break;
                                      }
                                  }
                              }
                          }
                      }
  
                      zipOutputStream.Finish();
                      zipOutputStream.Close();
                  }
  
                  Response.End();
              }
          }
      }
  </code></pre>
  
  <p>When I open the downloaded zip file I get:</p>
  
  <blockquote>
    <p>Unexpected end of archive.</p>
  </blockquote>
  
  <p>The zip should contain three files, but only contains one. The size is also 0..</p>
  
  <p><strong>What have I missed?</strong></p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>Should I specify .rar type when naming?</p>
  
  <p>Don't I need to set position for stream?</p>
  
  <p>Have I positioned Flush, Finish, Close and End functions correctly?</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>This code should work!?</p>
  
  <pre><code>    public ActionResult DownloadImages(DownloadImagesModel model)
      {
          if (ModelState.IsValid &amp;&amp; model.ImageIDs != null &amp;&amp; model.ImageIDs.Count &gt; 0)
          {
              var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
  
              var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
  
              if (images != null &amp;&amp; images.Count &gt; 0)
              {
                  var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials("name", "pw"), true);
                  var container = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("images-1");
  
                  using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
                  {
                      foreach (var image in images)
                      {
                          if (image.ImageLinks != null)
                          {
                              List&lt;ImageLinkModel&gt; imageLinks = new List&lt;ImageLinkModel&gt;();
  
                              if (model.FormatID == 0)
                                  imageLinks = image.ImageLinks.ToList();
                              else
                                  imageLinks = image.ImageLinks.Where(m =&gt; m.FormatID == model.FormatID).ToList();
  
                              if (imageLinks != null &amp;&amp; imageLinks.Count &gt; 0)
                              {
                                  zipOutputStream.SetLevel(0);
  
                                  var blob = container.GetBlockBlobReference(imageLinks.First().BlobName);
  
                                  var entry = new ZipEntry(imageLinks.First().BlobName);
                                  zipOutputStream.PutNextEntry(entry);
  
                                  //Download
                                  blob.DownloadToStream(zipOutputStream);
                              }
                          }
                      }
  
                      zipOutputStream.Finish();
                      zipOutputStream.Close();
                  }
  
                  Response.BufferOutput = false;
                  Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.zip");
                  Response.ContentType = "application/octet-stream";
                  Response.Flush();
                  Response.End();
                  return null;
              }
          }
  
          return null;
      }
  </code></pre>