protected override void FlushCore() { // Ensure that all the data has been read out of the package // stream already. Otherwise we'll lose data when we recreate the zip foreach (ZipPackagePart part in Parts.Values) { if (part.Package != null) { part.GetStream().Dispose(); } } // Empty the package stream PackageStream.Position = 0; PackageStream.SetLength(0); // Recreate the zip file using (ZipArchive archive = new ZipArchive(PackageStream, Append.Create, false)) { // Write all the part streams foreach (ZipPackagePart part in Parts.Values) { if (part.Package == null) { continue; } Stream partStream = part.GetStream(); partStream.Seek(0, SeekOrigin.Begin); using (Stream destination = archive.GetStream(part.Uri.ToString().Substring(1), part.CompressionOption)) { int count = (int)Math.Min(2048, partStream.Length); byte[] buffer = new byte [count]; while ((count = partStream.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, count); } } } using (Stream s = archive.GetStream(ContentUri, CompressionOption.Maximum)) WriteContentType(s); } }
protected override void FlushCore() { // Ensure that all the data has been read out of the package // stream already. Otherwise we'll lose data when we recreate the zip foreach (ZipPackagePart part in Parts.Values) { part.GetStream(); } if (!PackageStream.CanSeek) { return; } // Empty the package stream PackageStream.Position = 0; PackageStream.SetLength(0); // Recreate the zip file using (ZipStorer archive = ZipStorer.Create(PackageStream, "", false)) { // Write all the part streams foreach (ZipPackagePart part in Parts.Values) { Stream partStream = part.GetStream(); partStream.Seek(0, SeekOrigin.Begin); archive.AddStream(ZipStorer.Compression.Deflate, part.Uri.ToString().Substring(1), partStream, DateTime.UtcNow, ""); } using (var ms = new MemoryStream()) { WriteContentType(ms); ms.Seek(0, SeekOrigin.Begin); archive.AddStream(ZipStorer.Compression.Deflate, ContentUri, ms, DateTime.UtcNow, ""); } } }