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);
container.CreateIfNotExists();
// 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())
blob.UploadFromStream(fs);
}
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;
}
}
}
References:
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/
Comments
Anonymous
May 27, 2015
Upload, share, track, manage your files in one simple to use file host.<a href="http://uploads.xyz/"> upload, share, track, file, hosting, host</a>Anonymous
September 18, 2015
Hi, I am working on Azure Asp.net MVC web app. I have bolb directory of 755MB and i want to make it zip and want to upload zip file as blob. Kindly please suggest, how i can do this? i have tried to use DOTNETZIP and downloading all the blobs under than blob directory and adding those one by one in to zip.then i save zipfile to zipstream and use parallel upload by dividing into chunks to upload complete file as zip blob. it is working fine. but when it comes to run under azure environment, process stops and session timeout occurs. it does not throw any exception and in local code works perfectly fine. please help me.