From 62c5807f178c1f9fb2ab4041a659f735bfb2b9d6 Mon Sep 17 00:00:00 2001 From: Bob Arnson Date: Sat, 21 Sep 2024 22:52:22 -0400 Subject: [PATCH] Add overloads to support create-only Wixouts. This prevents the .NET ZipArchive (and friends) from keeping the whole thing in memory, to support updating when we don't need to update the Wixout when building a binary Wixlib. --- src/api/wix/WixToolset.Data/Intermediate.cs | 14 ++++++ src/api/wix/WixToolset.Data/WixOutput.cs | 43 +++++++++++++++++-- .../CommandLine/BuildCommand.cs | 2 +- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/api/wix/WixToolset.Data/Intermediate.cs b/src/api/wix/WixToolset.Data/Intermediate.cs index 977f894a4..5dc389803 100644 --- a/src/api/wix/WixToolset.Data/Intermediate.cs +++ b/src/api/wix/WixToolset.Data/Intermediate.cs @@ -246,6 +246,20 @@ public void Save(string path) } } + /// + /// Saves an intermediate that can only be written to to a path on disk. + /// + /// Path to save intermediate file to disk. + public void SaveNew(string path) + { + Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))); + + using (var wixout = WixOutput.CreateNew(path)) + { + this.Save(wixout); + } + } + /// /// Saves an intermediate to a WixOutput. /// diff --git a/src/api/wix/WixToolset.Data/WixOutput.cs b/src/api/wix/WixToolset.Data/WixOutput.cs index 43359f247..44882f566 100644 --- a/src/api/wix/WixToolset.Data/WixOutput.cs +++ b/src/api/wix/WixToolset.Data/WixOutput.cs @@ -25,7 +25,7 @@ private WixOutput(Uri uri, ZipArchive archive, Stream stream) } /// - /// + /// /// public Uri Uri { get; } @@ -60,6 +60,24 @@ public static WixOutput Create(string path) return WixOutput.Create(uri, stream); } + /// + /// Creates a new file structure on disk that can only be written to. + /// + /// Path to write file structure to. + /// Newly created WixOutput. + public static WixOutput CreateNew(string path) + { + var fullPath = Path.GetFullPath(path); + + Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); + + var uri = new Uri(fullPath); + + var stream = File.Create(path); + + return WixOutput.CreateNew(uri, stream); + } + /// /// Creates a new file structure. /// @@ -73,6 +91,19 @@ public static WixOutput Create(Uri uri, Stream stream) return new WixOutput(uri, archive, stream); } + /// + /// Creates a new file structure that can only be written to. + /// + /// + /// Stream to write the file structure to. + /// Newly created WixOutput. + public static WixOutput CreateNew(Uri uri, Stream stream) + { + var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true); + + return new WixOutput(uri, archive, stream); + } + /// /// Loads a wixout from a path on disk. /// @@ -189,7 +220,10 @@ public void ExtractEmbeddedFile(string embeddedId, string outputPath) /// Stream to the data of the file. public Stream CreateDataStream(string name) { - this.DeleteExistingEntry(name); + if (this.archive.Mode == ZipArchiveMode.Update) + { + this.DeleteExistingEntry(name); + } var entry = this.archive.CreateEntry(name); @@ -203,7 +237,10 @@ public Stream CreateDataStream(string name) /// Path to file on disk to include in the output. public void ImportDataStream(string name, string path) { - this.DeleteExistingEntry(name); + if (this.archive.Mode == ZipArchiveMode.Update) + { + this.DeleteExistingEntry(name); + } this.archive.CreateEntryFromFile(path, name, System.IO.Compression.CompressionLevel.Optimal); } diff --git a/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs b/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs index d07dd6aac..0db394805 100644 --- a/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs +++ b/src/wix/WixToolset.Core/CommandLine/BuildCommand.cs @@ -248,7 +248,7 @@ private void LibraryPhase(IReadOnlyCollection intermediates, IRead if (!this.Messaging.EncounteredError) { - result.Library.Save(outputPath); + result.Library.SaveNew(outputPath); this.LayoutFiles(result.TrackedFiles, null, cancellationToken); }