0

I have created a Web API method to download a blob file from azure storage and its working fine and I have written the code to download only one file, but I want to download many blob files as a zip file from Azure Blob Storage.

So my question here is simple that How to convert or make a zip file from memory stream? And I looped and got a memory stream List like below, but I don't know how to append this list to Zip and return as an HttpResponseMessage, Could anybody please help me to append multiple Memorystream to a zip file?

And please find the following methods that I have written for download a blob file from Azure.

API Method,

{
  var document = DataContext.Documents.FirstOrDefault(d => d.DocumentId == 
  documentId && d.IsActive `enter code here`);

  //azure storage connection
  CloudBlockBlob blob = GetBlobReferece(document);

  MemoryStream memStream = new MemoryStream();

 //downloads the memory stream of a 
 await blob.DownloadToStreamAsync(memStream);


        HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
        httpResponseMessage.Content = new ByteArrayContent(memStream.ToArray());
        httpResponseMessage.Content.Headers.Add("x-filename", document.FileName);
        httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = document.FileName;
        httpResponseMessage.StatusCode = HttpStatusCode.OK;

        return httpResponseMessage;
 }

Tried for Multiple Blob Files,

    List<MemoryStream> memStList = new List<MemoryStream>();
    for (int i = 1; i <= 3; i++)
    {
            CloudBlockBlob b = GetBlobReferece(documentList[i - 1]);
            MemoryStream ms = new MemoryStream();
            b.DownloadToStreamAsync(ms);
            memStList.Add(ms);

    } 
Md Aslam
  • 1,228
  • 8
  • 27
  • 61
  • 2
    The option I'd choose at a high level (since it has imo the best performance): write a custom action result that takes the identifiers for the blobs, creates a ZipArchive pointed at the response Stream, then start iterating the blobs and directly copying each blob's Stream into a Stream in an entry created in the archive. Don't use MemoryStream if you can, as it loads the whole file into memory which can be bad with large files/lot of requests. – juunas Aug 01 '18 at 18:06
  • 1
    As has been answered in other questions about this: there's no zip support in blob storage; that is, each object is a discrete object. You'd need to come up with your own scheme for collecting blob content and creating a zip file before downloading. – David Makogon Aug 01 '18 at 18:46
  • 1
    For example, look at the answer I posted to [this question](https://stackoverflow.com/questions/46323731/azure-php-sdk-download-containers-all-blob-in-a-single-zip-file) which asks the same thing (albeit with php vs c#) – David Makogon Aug 01 '18 at 18:47
  • @David Could you please give me a sample code for creating a zip file in C#? If you can please help me with this. – Md Aslam Aug 02 '18 at 04:20
  • @ David, I have edited my question so please re check the question and above given code. You commented to use ZipArchive pointing to the response stream by iterating blob, so I Iterated the blob like above given code, but I don't know how to move next step due to I am very new to these areas. So please help me with some sample codes. I will hit correct code for your help also. Thanks :) – Md Aslam Aug 02 '18 at 10:17
  • @Junnas Could you please help me to "copying each blob's Stream into a Stream in an entry created in the archive" and to "return the zip file as HttpContent" ? – Md Aslam Aug 02 '18 at 13:18

0 Answers0