aboutsummaryrefslogtreecommitdiff
path: root/src/api/wix/WixToolset.Data/WixOutput.cs
blob: 43359f2474c6c65c51c770a189c0d810b1db6f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Data
{
    using System;
    using System.IO;
    using System.IO.Compression;
    using System.Reflection;
    using System.Text;

    /// <summary>
    /// Class that understands the standard file structure of the WiX toolset.
    /// </summary>
    public class WixOutput : IDisposable
    {
        private readonly Stream stream;
        private ZipArchive archive;
        private bool disposed;

        private WixOutput(Uri uri, ZipArchive archive, Stream stream)
        {
            this.Uri = uri;
            this.archive = archive;
            this.stream = stream;
        }

        /// <summary>
        /// 
        /// </summary>
        public Uri Uri { get; }

        /// <summary>
        /// Creates a new file structure in memory.
        /// </summary>
        /// <returns>Newly created <c>WixOutput</c>.</returns>
        public static WixOutput Create()
        {
            var uri = new Uri("memorystream:");

            var stream = new MemoryStream();

            return WixOutput.Create(uri, stream);
        }

        /// <summary>
        /// Creates a new file structure on disk.
        /// </summary>
        /// <param name="path">Path to write file structure to.</param>
        /// <returns>Newly created <c>WixOutput</c>.</returns>
        public static WixOutput Create(string path)
        {
            var fullPath = Path.GetFullPath(path);

            Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

            var uri = new Uri(fullPath);

            var stream = File.Create(path);

            return WixOutput.Create(uri, stream);
        }

        /// <summary>
        /// Creates a new file structure.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="stream">Stream to write the file structure to.</param>
        /// <returns>Newly created <c>WixOutput</c>.</returns>
        public static WixOutput Create(Uri uri, Stream stream)
        {
            var archive = new ZipArchive(stream, ZipArchiveMode.Update, leaveOpen: true);

            return new WixOutput(uri, archive, stream);
        }

        /// <summary>
        /// Loads a wixout from a path on disk.
        /// </summary>
        /// <param name="path">Path to wixout file saved on disk.</param>
        /// <returns>Loaded created <c>WixOutput</c>.</returns>
        public static WixOutput Read(string path)
        {
            var uri = new Uri(Path.GetFullPath(path));

            var stream = File.OpenRead(path);

            return Read(uri, stream);
        }

        /// <summary>
        /// Loads a wixout from a path on disk or embedded resource in assembly.
        /// </summary>
        /// <param name="baseUri">Uri with local path to wixout file saved on disk or embedded resource in assembly.</param>
        /// <returns>Loaded created <c>WixOutput</c>.</returns>
        public static WixOutput Read(Uri baseUri)
        {
            // If the embedded files are stored in an assembly resource stream (usually
            // a .wixlib embedded in a WixExtension).
            if ("embeddedresource" == baseUri.Scheme)
            {
                var assemblyPath = Path.GetFullPath(baseUri.LocalPath);
                var resourceName = baseUri.Fragment.TrimStart('#');

                var assembly = Assembly.LoadFile(assemblyPath);
                return WixOutput.Read(assembly, resourceName);
            }
            else // normal file (usually a binary .wixlib on disk).
            {
                var stream = File.OpenRead(baseUri.LocalPath);
                return WixOutput.Read(baseUri, stream);
            }
        }

        /// <summary>
        /// Loads a wixout from an assembly resource stream.
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns>Loaded created <c>WixOutput</c>.</returns>
        public static WixOutput Read(Assembly assembly, string resourceName)
        {
            var resourceStream = assembly.GetManifestResourceStream(resourceName);

            var uriBuilder = new UriBuilder(assembly.CodeBase)
            {
                Scheme = "embeddedresource",
                Fragment = resourceName
            };

            return Read(uriBuilder.Uri, resourceStream);
        }

        /// <summary>
        /// Reads a file structure from an open stream.
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="stream">Stream to read from.</param>
        /// <returns>Loaded created <c>WixOutput</c>.</returns>
        public static WixOutput Read(Uri uri, Stream stream)
        {
            try
            {
                var archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);

                return new WixOutput(uri, archive, stream);
            }
            catch (InvalidDataException)
            {
                throw new WixException(ErrorMessages.CorruptFileFormat(uri.AbsoluteUri, "wixout"));
            }
        }

        /// <summary>
        /// Reopen the underlying archive for read-only or read-write access.
        /// </summary>
        /// <param name="writable">Indicates whether the output can be modified. Defaults to false.</param>
        public void Reopen(bool writable = false)
        {
            this.archive?.Dispose();
            this.archive = null;

            this.archive = new ZipArchive(this.stream, writable ? ZipArchiveMode.Update : ZipArchiveMode.Read, leaveOpen: true);
        }

        /// <summary>
        /// Extracts an embedded file.
        /// </summary>
        /// <param name="embeddedId">Id to the file to extract.</param>
        /// <param name="outputPath">Path to write the extracted file to.</param>
        public void ExtractEmbeddedFile(string embeddedId, string outputPath)
        {
            var entry = this.archive.GetEntry(embeddedId);

            if (entry == null)
            {
                throw new ArgumentOutOfRangeException(nameof(embeddedId));
            }

            var folder = Path.GetDirectoryName(outputPath);

            Directory.CreateDirectory(folder);

            entry.ExtractToFile(outputPath, overwrite: true);
        }

        /// <summary>
        /// Creates a data stream in the wixout.
        /// </summary>
        /// <returns>Stream to the data of the file.</returns>
        public Stream CreateDataStream(string name)
        {
            this.DeleteExistingEntry(name);

            var entry = this.archive.CreateEntry(name);

            return entry.Open();
        }

        /// <summary>
        /// Imports a file from disk into the output.
        /// </summary>
        /// <param name="name">Name of the stream in the output.</param>
        /// <param name="path">Path to file on disk to include in the output.</param>
        public void ImportDataStream(string name, string path)
        {
            this.DeleteExistingEntry(name);

            this.archive.CreateEntryFromFile(path, name, System.IO.Compression.CompressionLevel.Optimal);
        }

        /// <summary>
        /// Gets a non-closing stream to the data of the file.
        /// </summary>
        /// <returns>Stream to the data of the file.</returns>
        public Stream GetDataStream(string name)
        {
            var entry = this.archive.GetEntry(name);

            if (entry == null)
            {
                throw new ArgumentOutOfRangeException(nameof(name));
            }

            return entry.Open();
        }

        /// <summary>
        /// Gets the data of the file as a string.
        /// </summary>
        /// <returns>String contents data of the file.</returns>
        public string GetData(string name)
        {
            var entry = this.archive.GetEntry(name);

            // Use StreamReader to "swallow" BOM if present.
            using (var stream = entry.Open())
            using (var streamReader = new StreamReader(stream, Encoding.UTF8))
            {
                return streamReader.ReadToEnd();
            }
        }

        /// <summary>
        /// Disposes of the internal state of the file structure.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes of the internsl state of the file structure.
        /// </summary>
        /// <param name="disposing">True if disposing.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    this.archive?.Dispose();
                    this.stream?.Dispose();
                }
            }

            this.disposed = true;
        }

        private void DeleteExistingEntry(string name)
        {
            var entry = this.archive.GetEntry(name);
            if (entry != null)
            {
                entry.Delete();
            }
        }
    }
}