unable to update zip file in Azure File Share/Blob

362 Views Asked by At

I am using Azure File share I want to create zip file only once but wants to update it multiple times (upload multiple files after once created). is it possible to create .zip file only once and add more files in it later without **overriding **existing files in zip.? when i tried to add more files in .zip it overrides existing files in zip with new file.

private static async Task OpenZipFile()
    {
        try
        {



            using (var zipFileStream = await OpenZipFileStream())
            {
                using (var zipFileOutputStream = CreateZipOutputStream(zipFileStream))
                {
                    var level = 0;
                    zipFileOutputStream.SetLevel(level);


                    BlobClient blob = new BlobClient(new Uri(String.Format("https://{0}.blob.core.windows.net/{1}", "rtsatestdata", "comm/2/10029.txt")), _currentTenantTokenCredential);

                    var zipEntry = new ZipEntry("newtestdata")
                    {
                        Size = 1170
                    };
                    zipFileOutputStream.PutNextEntry(zipEntry);

                    blob.DownloadToAsync(zipFileOutputStream).Wait();
                    zipFileOutputStream.CloseEntry();
                }
            }
        }
        catch (TaskCanceledException)
        {

            throw;
        }
    }


private static async Task<Stream> OpenZipFileStream()
    {

BlobContainerClient mainContainer = _blobServiceClient.GetBlobContainerClient("comm");

        var blobItems = mainContainer.GetBlobs(BlobTraits.Metadata, BlobStates.None);

        foreach (var item in blobItems)
        {
            if (item.Name == "testdata.zip")
            {
                BlobClient blob = new BlobClient(new Uri(String.Format("https://{0}.blob.core.windows.net/{1}", "rtsatestdata", "comm/testdata.zip")), _currentTenantTokenCredential);

               return await blob.OpenWriteAsync(true
                    , options: new BlobOpenWriteOptions
                    {
                        HttpHeaders = new BlobHttpHeaders
                        {
                            ContentType = "application/zip"
                        }
                    }
                );

            }
        }
}

private static ZipOutputStream CreateZipOutputStream(Stream zipFileStream)
    {
        return new ZipOutputStream(zipFileStream)
        {
            IsStreamOwner = false,
        };
    }
1

There are 1 best solutions below

0
On

This is not possible in Azure storage. The workaround would be to download the zip, unzip it, add more files, re-zip it, and re-upload to storage.