我在代码中发现了-回顾起来很明显的错误。必须 处理
ZipArchive,以使其内容写入其基础流。因此,在ZipArchive的using块结束后,我不得不将流保存到文件中。
而且重要的是将其构造函数的 LeaveOpen 参数设置为true,以使其不关闭基础流。因此,这是完整的工作解决方案:
using (MemoryStream zipStream = new MemoryStream()){ using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create, true)) { var entry = zip.CreateEntry("test.txt"); using (StreamWriter sw = new StreamWriter(entry.Open())) { sw.WriteLine( "Etiam eros nunc, hendrerit nec malesuada vitae, pretium at ligula."); } } var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync( "test.zip", CreationCollisionOption.ReplaceExisting); zipStream.Position = 0; using (Stream s = await file.OpenStreamForWriteAsync()) { zipStream.CopyTo(s); }}


