aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Bind/ExtractEmbeddedFilesCommand.cs
blob: 68bfd8d7d7a68cecc75a8b6f1b9874bc6f79d6fa (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
// 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.Bind
{
    using System.IO;
    using System.Reflection;
    using WixToolset.Data;

    internal class ExtractEmbeddedFilesCommand : ICommand
    {
        public ExtractEmbeddedFiles FilesWithEmbeddedFiles { private get; set; }

        public void Execute()
        {
            foreach (var baseUri in this.FilesWithEmbeddedFiles.Uris)
            {
                Stream stream = null;
                try
                {
                    // If the embedded files are stored in an assembly resource stream (usually
                    // a .wixlib embedded in a WixExtension).
                    if ("embeddedresource" == baseUri.Scheme)
                    {
                        string assemblyPath = Path.GetFullPath(baseUri.LocalPath);
                        string resourceName = baseUri.Fragment.TrimStart('#');

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

                    using (FileStructure fs = FileStructure.Read(stream))
                    {
                        foreach (var embeddedFile in this.FilesWithEmbeddedFiles.GetExtractFilesForUri(baseUri))
                        {
                            fs.ExtractEmbeddedFile(embeddedFile.EmbeddedFileIndex, embeddedFile.OutputPath);
                        }
                    }
                }
                finally
                {
                    if (null != stream)
                    {
                        stream.Close();
                    }
                }
            }
        }
    }
}