I'm trying to take an existing .zip file (I get it from an api as byte array), take some files from it, and make a new .zip file. Then return that new .zip file as byte array. The catch is that all of this should be done in memory, without writing any actual files to disk.
So I have this setup. First memory stream and ziparchive is used for the orignal zip file. Second stream and archive represents the second zip that will only contained filtered items from the first zip.
using var downloadedRepoStream = new MemoryStream(binaryRepo);
using var downloadedRepoArchive = new ZipArchive(downloadedRepoStream, ZipArchiveMode.Read, true);
var entriesToExtract = downloadedRepoArchive.Entries; //add some filtering
using var subRepoStream = new MemoryStream();
using var subRepoArchive = new ZipArchive(subRepoStream, ZipArchiveMode.Create, true);
Then I go through each entry and copy contents over to the second zip.
foreach (var entry in entriesToExtract)
{
var entryName = entry.FullName.Split('/', 2)[1];
using var entryStream = entry.Open();
var subRepoEntry = subRepoArchive.CreateEntry(entry.FullName);
using var subRepoEntryStream = subRepoEntry.Open();
await entryStream.CopyToAsync(subRepoEntryStream);
}
Finally, I need to return that second ziparchive as byte array. I assumed I that I could just call MemoryStream.ToArray(), but the MemoryStream, used by the second ZipArchive, remains empty, despite ZipArchive having files. So how do I get byte[] from ZipArchive?