Kathir Subramaniam
Kathir Subramaniam

Reputation: 1245

How to unzip Self Extracting Zip files in Azure Blob Storage?

I have a zip file(.Exe - Self-extracting zip file) that can be extracted using 7zip. As I want to automate the extraction process, I used the below C# code. It is working for the normal 7z files. But facing this issue 'Cannot access the closed Stream', when I trying to extract the specific self-extracting (.Exe) zip file. Fyi. Manually I ensured the 7zip command line version is unzipping the file.

using (SevenZipExtractor extract = new SevenZipExtractor(zipFileMemoryStream))
    {
        foreach (ArchiveFileInfo archiveFileInfo in extract.ArchiveFileData)
        {
            if (!archiveFileInfo.IsDirectory)
            {
                using (var memory = new MemoryStream())
                {
                    string shortFileName = Path.GetFileName(archiveFileInfo.FileName);
                    extract.ExtractFile(archiveFileInfo.Index, memory);
                    byte[] content = memory.ToArray();
                    file = new MemoryStream(content);
                }
            }
        }
    }

The zip file is in Azure blob storage. I dont know how to get the extracted files in the blob storage.

Upvotes: 1

Views: 1409

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8254

Here is one of the workarounds that has worked for me. Instead of 7Zip I have used ZipArchive.

ZipArchive archive = new ZipArchive(myBlob);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(destinationStorage);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(destinationContainer);

foreach(ZipArchiveEntry entry in archive.Entries) {
  log.LogInformation($"Now processing {entry.FullName}");

  string valideName = Regex.Replace(entry.Name, @ "[^a-zA-Z0-9\-]", "-").ToLower();

  CloudBlockBlob blockBlob = container.GetBlockBlobReference(valideName);
  using(var fileStream = entry.Open()) {
    await blockBlob.UploadFromStreamAsync(fileStream);
  }
}

REFERENCE: How to Unzip Automatically your Files with Azure Function v2

Upvotes: 1

Related Questions