I have a Background Worker Process, where I need to download multiple files from Azure Storage and then create a ZIP file with those files. All the process will be done in memory.
So, I created the below code.
SemaphoreSlim semaphore = new(1, 1);
ParallelOptions options = new() { MaxDegreeOfParallelism = 10 };
using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Update, false))
{
Parallel.ForEach(supportingDocLists, options, supportingDoc =>
{
if (!string.IsNullOrWhiteSpace(supportingDoc.FileURL) && !string.IsNullOrWhiteSpace(supportingDoc.FileName))
{
BlobClient blobClient = containerClient.GetBlobClient(supportingDoc.FileURL);
if (blobClient.Exists())
{
BlobDownloadInfo downloadInfo = blobClient.Download();
// Store it in the zip file sequentially.
//semaphore.Wait();
ZipArchiveEntry zipEntry = zipArchive.CreateEntry(supportingDoc.FileName);
lock(zipEntry)
{
using (Stream entryStream = zipEntry.Open())
{
blobClient.DownloadTo(entryStream);
}
}
}
}
});
}
zipStream.Position = 0;
return zipStream.ToArray();
But the code is not working properly. I am having the exception (Cannot access a closed stream). Can anyone help me to resolve the problem.
