From f82e42264ce390940a430f4fb31ebc7f58bdd7d4 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 | 32 +++++++++++++++++-- .../CommandLine/BuildCommand.cs | 2 +- 3 files changed, 44 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..72b922c9e 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; } @@ -189,7 +189,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 +206,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); } @@ -240,6 +246,26 @@ public string GetData(string name) } } + /// + /// Creates a new file structure on disk that can only be written to. + /// + /// Path to write file structure to. + /// Newly created WixOutput. + internal 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); + + var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true); + + return new WixOutput(uri, archive, stream); + } + /// /// Disposes of the internal state of the file structure. /// 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); }