C# Code to Zip Files and Upload to Azure Storage

If you need code to zip some files into an archive and upload it to Azure blob storage, here is a sample for you. Once the data is in Azure Storage, you could use the WebJobs SDK to trigger your Azure application to work on it (for example, ETL into Azure SQL Database).

Create a new console C# application project in Visual Studio, add references to System.IO.Compression and System.IO.Compression.FileSystem, then add the Windows Azure Storage NuGet package to your project.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

namespace ZipCopySample
    class Program
        private const string connectionString = "DefaultEndpointsProtocol=https;AccountName=afilestore;AccountKey=tdiU7UJaYOUCANTHAVEMYKEYD7p8qKQE7U27vgNUN4ZRbK0hvlriA5YZMujL/w==";

        static void Main(string[] args)
            string[] filesToZip = new string[] { @"c:\temp\table.txt", @"c:\temp\bcp.fmt" };
             FileInfo zipFile = CreateZip(@"C:\temp\myzip.zip", filesToZip);

            UploadToBlobStorage(zipFile, connectionString, "sqldata");


        private static void UploadToBlobStorage(FileInfo zipFile, string storageConnectionString, string blobContainerName)
            // Connect to the storage account's blob endpoint
            CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient client = account.CreateCloudBlobClient();

            // Create the blob storage container
             CloudBlobContainer container = client.GetContainerReference(blobContainerName);

            // Create the blob in the container
             CloudBlockBlob blob = container.GetBlockBlobReference(zipFile.Name);

            // Upload the zip and store it in the blob
            using (FileStream fs = zipFile.OpenRead())

        private static FileInfo CreateZip(string zipFileName, string[] filesToZip)
            FileInfo zipFile = new FileInfo(zipFileName);
            FileStream fs = zipFile.Create();
             using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create))
                foreach(string fileName in filesToZip)
                    zip.CreateEntryFromFile(fileName, Path.GetFileName(fileName), CompressionLevel.Optimal);

            return zipFile;



ZipArchive Class: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx

How to use Blob storage from .NET: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/

How to use Azure blob storage with the WebJobs SDK: https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-storage-blobs-how-to/