我正在使用 ZipArchive + FileStream 创建一个 zip 文件。当新项目添加到 zip 文件中时,我想将新添加的项目刷新/写入到 zip 流下面。
下面的代码不会刷新单个 zip 项目。当 FileStream 处理时,整个 zip 被写入 output.zip。
var files = Directory.GetFiles("C:\\Temp","*.pdf");
using (var output = new FileStream("c:\\temp\\output.zip", FileMode.Create, FileAccess.Write))
{
using (System.IO.Compression.ZipArchive zip = new ZipArchive(output, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
using (var internalFile = new FileStream(file, FileMode.Open))
{
var zipItem = zip.CreateEntry(Path.GetFileName(file));
using var entryStream = zipItem.Open();
{
await internalFile.CopyToAsync(entryStream).ConfigureAwait(false);
}
}
await output.FlushAsync();
// after each file flush the output stream.
// expectation at this point, individual zip item will be written to physical file.
// however I don't see the file size changes in windows explorer.
} // put breakpoint here
}
} // The whole output get flush at this point when FileStream is disposed