I have a text file with hundreds of thousands of image urls. I want to download all these images and put them in one zip file.
I can get each image and add it to the zip directly one by one without saving it to disk:
using StreamReader reader = new StreamReader("UrlList.txt");
while ((_url = reader.ReadLine()) != null)
{
using var responseStream = await _httpClient.GetStreamAsync(_url);
using var zipStream = new FileStream("images.zip", FileMode.OpenOrCreate);
using var zip = new ZipArchive(zipStream, ZipArchiveMode.Update);
var file = zip.CreateEntry(fileName);
using var fileStream = file.Open();
responseStream.CopyTo(fileStream);
}
but it looks like an inefficient way (40k images in ~4,5h in my case). And I am not sure there is no memory leak. ZipArchive does not allow adding files in parallel.
Is there any way to do this efficiently?