C# Azure.Storage.Blobs SDK How to list and zip all files in a container and store the zip in another container

Viewed 332

I have around 100 files in a container . I want to call a function to zip all files and send it to another container . I am using the Azure.Storage.Blobs version 12.9.1

var blob = container.GetBlockBlobReference(outputFilename);
using (var stream = await blob.OpenWriteAsync())
using (var zip = new ZipArchive(stream, ZipArchiveMode.Create))
{
    for (int i = 0; i < 2000; i++)
    {
        using (var randomStream = CreateRandomStream(2))
        {
            var entry = zip.CreateEntry($"{i}.zip", CompressionLevel.Optimal);
            using (var innerFile = entry.Open())
            {
                await randomStream.CopyToAsync(innerFile);
            }
        }
    }
}

This is for the older version . How can we achieve it using the new sdk

1 Answers

Can you please try with this code.

Solution 1: Try this code to generate Zip file. After generating zip file upload it to container

public void ZipFilesToResponse(HttpResponseBase response, IEnumerable<Asset> files, string zipFileName)
{
    using (var zipOutputStream = new ZipOutputStream(response.OutputStream))
    {
        zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
        response.BufferOutput = false;
        response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
        response.ContentType = "application/octet-stream";

        foreach (var file in files)
        {
            var entry = new ZipEntry(file.FilenameSlug())
            {
                DateTime = DateTime.Now,
                Size = file.Filesize
            };
            zipOutputStream.PutNextEntry(entry);
            storageService.ReadToStream(file, zipOutputStream);
            response.Flush();
            if (!response.IsClientConnected)
            {
                break;
            }
        }
        zipOutputStream.Finish();
        zipOutputStream.Close();
    }
    response.End();
}

For more details refer this link

Solution 2:Also try with this code

using System;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace SO68566758
{
    class Program
    {
        private const string connectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
        private const string sourceContainer = "source-container";
        private const string targetContainer = "target-container";
        private const string outputBlobName = "backup.zip";
        static async Task Main(string[] args)
        {
            BlobServiceClient serviceClient = new BlobServiceClient(connectionString);
            BlobContainerClient sourceContainerClient = serviceClient.GetBlobContainerClient(sourceContainer);
            BlobContainerClient targetContainerClient = serviceClient.GetBlobContainerClient(targetContainer);
            var blobs = sourceContainerClient.GetBlobsAsync();
            using (var fs = new FileStream(outputBlobName, FileMode.OpenOrCreate))
            {
                using (var zipOutputStream = new ZipOutputStream(fs))
                {
                    await foreach (var blob in blobs)
                    {
                        var blobName = blob.Name;
                        var blobClient = sourceContainerClient.GetBlobClient(blobName);
                        //var downloadResponse = await blobClient.DownloadAsync();
                        //var streamContent = downloadResponse.Value.Content;
                        var entry = new ZipEntry(blobName);
                        zipOutputStream.PutNextEntry(entry);
                        await blobClient.DownloadToAsync(zipOutputStream);
                    }
                }
            }

            BlobClient targetBlob = targetContainerClient.GetBlobClient(outputBlobName);
            using (FileStream fs = new FileStream(outputBlobName, FileMode.Open))
            {
                await targetBlob.UploadAsync(fs);
            }
        }
    }
}
Related