Search code examples
azureazure-storage

How can I dynamically create Ziparchive in Azure


I have multiple files in my Azure storage account say its Master container, I have created a dynamic container which will have the required files copied from Master Container, and those coped files needed to be zipped inside that created container. See below code, where in I have created the zip archive. Also the zip archive is getting created, but when I download manually and see the files, it seems corrupted (for ex. the actual size of the individual files are more than 1Mb but the files which I get after download seems 22Kb), and File formats would be .ipt, .iam (Autodesk Inventor Files)

CloudBlobContainer destContainer = blobClient.GetContainerReference(AzureContainer);
            bool isCreated = destContainer.CreateIfNotExists();
            var zipblob = destContainer.GetBlockBlobReference("inputAssembly.zip");
            using (var stream = await zipblob.OpenWriteAsync())
            using (var zip = new ZipArchive(stream, ZipArchiveMode.Create))
            {
                foreach (var fileName in inputfile)
                {
                    using (var fileStream = new MemoryStream())
                    {
                        if (destContainer.GetBlockBlobReference(fileName).Exists())
                        {
                            destContainer.GetBlockBlobReference(fileName).DownloadToStream(fileStream);
                        }
                        var newZip = new ZipArchive(fileStream, ZipArchiveMode.Create);
                        var entry = newZip.CreateEntry(fileName, CompressionLevel.NoCompression);
                        using (var innerFile = entry.Open())
                        {
                            fileStream.CopyTo(innerFile);
                        }
                        fileStream.Close();
                    }
                }
            }
        

Solution

  •         CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(fixedpartContainer);
            CloudBlobContainer destContainer1 = blobClient.GetContainerReference(AzureContainer);
            bool isCreated = destContainer1.CreateIfNotExists();
            var zipblob = destContainer1.GetBlockBlobReference("inputAssembly.zip");
            using (var stream = await zipblob.OpenWriteAsync())
            {
                using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create))
                {
                    foreach (var blobName in blobFileNames)
                    {
                        var blob = destContainer.GetBlockBlobClient(blobName);
                        var zipEntry = zipArchive.CreateEntry(blobName);
                        using var zipStream = zipEntry.Open();
                        using var fileStream = new MemoryStream();
                        await blob.DownloadToAsync(fileStream);
                        await zipStream.WriteAsync(fileStream.ToArray());
                        AssemblyCreated = true;
    
                    }
                }
            }