davenewza davenewza - 8 hours ago 2
C# Question

Extract ZipArchive entry to blob storage

Using the normal Windows file system, the

ExtractToFile
method would be sufficient:

using (ZipArchive archive = new ZipArchive(uploadedFile.InputStream, ZipArchiveMode.Read, true))
{
foreach (var entry in archive.Entries.Where(x => x.Length > 0))
{
entry.ExtractToFile(Path.Combine(location, entry.Name));
}
}


Now that we are using Azure, this obviously needs to change as we are using blob storage.

How can this be done?

Answer

ZipArchiveEntry class has an Open method which returns a stream. What you could do is create a blob using that stream.

static void ZipArchiveTest()
        {
            storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("temp");
            container.CreateIfNotExists();
            var zipFile = @"D:\node\test2.zip";
            using (FileStream fs = new FileStream(zipFile, FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(fs))
                {
                    var entries = archive.Entries;
                    foreach (var entry in entries)
                    {
                        CloudBlockBlob blob = container.GetBlockBlobReference(entry.FullName);
                        using (var stream = entry.Open())
                        {
                            blob.UploadFromStream(stream);
                        }
                    }
                }
            }
        }