Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 714
Dave New Avatar asked Feb 16 '23 01:02

Dave New


1 Answers

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);
                        }
                    }
                }
            }
        }
like image 128
Gaurav Mantri Avatar answered Feb 24 '23 14:02

Gaurav Mantri