main
cs 73 lines 2.51 KB
Raw
1 // 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.
2
3 namespace WixToolset.Core.Burn.Bundles
4 {
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Linq;
9 using WixToolset.Core.Native;
10 using WixToolset.Data;
11 using WixToolset.Data.Symbols;
12
13 /// <summary>
14 /// Creates cabinet files.
15 /// </summary>
16 internal class CreateContainerCommand
17 {
18 public CreateContainerCommand(IEnumerable<WixBundlePayloadSymbol> payloads, string outputPath, CompressionLevel? compressionLevel)
19 {
20 this.Payloads = payloads;
21 this.OutputPath = outputPath;
22 this.CompressionLevel = compressionLevel;
23 }
24
25 public CreateContainerCommand(string manifestPath, IEnumerable<WixBundlePayloadSymbol> payloads, string outputPath, CompressionLevel? compressionLevel)
26 {
27 this.ManifestFile = manifestPath;
28 this.Payloads = payloads;
29 this.OutputPath = outputPath;
30 this.CompressionLevel = compressionLevel;
31 }
32
33 private CompressionLevel? CompressionLevel { get; }
34
35 private string ManifestFile { get; }
36
37 private string OutputPath { get; }
38
39 private IEnumerable<WixBundlePayloadSymbol> Payloads { get; }
40
41 public string Hash { get; private set; }
42
43 public long Size { get; private set; }
44
45 public void Execute()
46 {
47 var cabinetPath = Path.GetFullPath(this.OutputPath);
48 var cabinetFolder = Path.GetDirectoryName(cabinetPath);
49
50 Directory.CreateDirectory(cabinetFolder);
51
52 var files = new List<CabinetCompressFile>();
53
54 // If a manifest was provided always add it as "payload 0" to the container.
55 if (!String.IsNullOrEmpty(this.ManifestFile))
56 {
57 files.Add(new CabinetCompressFile(this.ManifestFile, "0"));
58 }
59
60 files.AddRange(this.Payloads.Select(p => new CabinetCompressFile(p.SourceFile.Path, p.EmbeddedId)));
61
62 var cab = new Cabinet(cabinetPath);
63 cab.Compress(files, this.CompressionLevel ?? Data.CompressionLevel.Medium);
64
65 // Now that the container is created, set the outputs of the command.
66 var fileInfo = new FileInfo(cabinetPath);
67
68 this.Hash = BundleHashAlgorithm.Hash(fileInfo);
69
70 this.Size = fileInfo.Length;
71 }
72 }
73 }