1

I am looping through the file names from my database and the same file i have in azure storage. I am zipping those n number of files and download from azure storage. I saves the zipped file to my local storage. When i extract and want to see a file, it say damaged/corrupt.

public ActionResult Download(string productid, string YearActiveid)
        {
            HomePageModel homepagemodel = new HomePageModel();

            homepagemodel.ProdHeaderDetail = GetProductHeaderDetail(productid, YearActiveid);
            homepagemodel.PriorYearsActive = GetPriorYearActive(productid, YearActiveid);

            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<name>;AccountKey=<key>;EndpointSuffix=core.windows.net");
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("product");

            var blobFileNames = new string[] { "file1.png", "file2.png", "file3.png", "file4.png" };
            var outputMemStream = new MemoryStream();
            var zipOutputStream = new ZipOutputStream(outputMemStream);
                foreach (var ProdHeaderDetail in homepagemodel.ProdHeaderDetail)
                {
                    zipOutputStream.SetLevel(5);
                    var blob = cloudBlobContainer.GetBlockBlobReference(ProdHeaderDetail.FileName);
                    var entry = new ZipEntry(ProdHeaderDetail.FileName);
                    zipOutputStream.PutNextEntry(entry);
                    blob.DownloadToStreamAsync(zipOutputStream);
                }


                zipOutputStream.Finish();
            //zipOutputStream.Close();
            //zipOutputStream.CloseEntry();
            zipOutputStream.IsStreamOwner = false;
                 outputMemStream.Position = 0;
                return File(outputMemStream, "application/zip", "filename.zip");

        }
bbajwa9366
  • 21
  • 1
  • 6
  • 1
    FYI you just gave away your storage account credentials to the world. Please refresh your storage account key immediately. I edited them out of your question but anyone with enough rep can still see the original. – David Makogon Jun 03 '20 at 15:56
  • Aside from that: Please provide more details about exactly what's happening. Otherwise, this question requires debugging by the community. We don't know anything about where your original content is, that you're putting into the zip file. And we don't know any other details, aside from you being unable to create the zip file with a zero-file-size error. – David Makogon Jun 03 '20 at 16:00
  • When i extract the zip file and tried to open the file (Adobe acrobat) it say damaged/corrupt file – bbajwa9366 Jun 03 '20 at 16:16
  • Right - I got that from your original description. I'm suggesting adding further details about how you even constructed the zip file to begin with. There are simply too many unknowns at this point. Please edit your question to add details (and please don't add details in comments). – David Makogon Jun 03 '20 at 16:42
  • Also: I don't know why you removed my edits, after I fixed your formatting issues, edited out your connection credentials, and removed unneeded ***** characters in your description. I had made your question properly formatted and easier to read. As such, I rolled back the edits. – David Makogon Jun 03 '20 at 16:45

1 Answers1

1

I resolved the issue by adding async and wait

public async Task Download(string productid, string YearActiveid)

await blob.DownloadToStreamAsync(zipOutputStream);

bbajwa9366
  • 21
  • 1
  • 6