Monday, July 14, 2014

Generate a Zip file at run time from Azure Blob Storage

Generate a Zip file at run time from Azure Blob Storage


Introduction
Many a times we provide options to Update/download/upload file in applications. These files can be located at physical servers or on Azure at some blob storage. When the files are on Azure and you need to download multiple files, it would always be a good approach to
·         download the stream of these files
·         zip them
·         download the zip format containing all files

Solution

There are several solutions to this. One solution I am going to explain is using the SharpZipLib dll.
Referring the definition from SharpZipLib site “#ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language)…..Read more
You can embed this library in to the solution by performing following steps:
1.       Right click the solution where you will write the code for zipping the files.
2.       Click Manage Package
3.       Find the shaprziplib dll in search text and then click Install.
Another way to add the library to your solution is running the following command in the Package Manager Console. The steps to use the Package Manager Console are described here.
Once you added the SharpZipLib dll reference to your solution, follow below steps for code to download and zip files:
Add the using statement in the class where you are adding the DownloadAll Code

using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

Add below using statement for Azure
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;




Then add following method

public void DownnloadStreamToZip(List<string> files, string BlobContainer)
        {
            CloudStorageAccount storageAcc;
            storageAcc = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureConnection"].ConnectionString);
            var storageHelper = new AzureStorageHelper();
            using (var zOutStream = new ZipOutputStream(Response.OutputStream))
            {
                zOutStream.SetLevel(0);
                Response.BufferOutput = false;
                Response.AppendHeader("content-disposition", "attachment; filename=AllFiles"".zip");
                Response.ContentType = "application/octet-stream";

                foreach (var file in files)
                {
                    var entry = new ZipEntry(file)
                    {
                        DateTime = DateTime.Now,
                    };
                    zOutStream.PutNextEntry(entry);
                    storageHelper.ReadToStream(BlobContainer, file, zOutStream);

                    Response.Flush();
                    if (!Response.IsClientConnected)
                    {
                        break;
                    }
                }
                zOutStream.Finish();
                zOutStream.Close();
            }
            Response.End();
        }


Call the DownnloadStreamToZip method from the event you want to trigger the Download All action, the method will download all the files from the specified BlobContainer.

Happy Coding!

No comments:

Post a Comment