aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/FileStructure.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/FileStructure.cs')
-rw-r--r--src/WixToolset.Data/FileStructure.cs65
1 files changed, 43 insertions, 22 deletions
diff --git a/src/WixToolset.Data/FileStructure.cs b/src/WixToolset.Data/FileStructure.cs
index 7265a51d..9ad9405a 100644
--- a/src/WixToolset.Data/FileStructure.cs
+++ b/src/WixToolset.Data/FileStructure.cs
@@ -7,6 +7,7 @@ namespace WixToolset.Data
7 using System.Diagnostics; 7 using System.Diagnostics;
8 using System.IO; 8 using System.IO;
9 using System.Linq; 9 using System.Linq;
10 using System.Text;
10 11
11 /// <summary> 12 /// <summary>
12 /// Class that understands the standard file structures in the WiX toolset. 13 /// Class that understands the standard file structures in the WiX toolset.
@@ -20,6 +21,8 @@ namespace WixToolset.Data
20 21
21 private static readonly Dictionary<string, FileFormat> SupportedFileFormats = new Dictionary<string, FileFormat>() 22 private static readonly Dictionary<string, FileFormat> SupportedFileFormats = new Dictionary<string, FileFormat>()
22 { 23 {
24 { "wir", FileFormat.WixIR },
25 { "wixirf", FileFormat.WixIR },
23 { "wixobj", FileFormat.Wixobj }, 26 { "wixobj", FileFormat.Wixobj },
24 { "wixlib", FileFormat.Wixlib }, 27 { "wixlib", FileFormat.Wixlib },
25 { "wixout", FileFormat.Wixout }, 28 { "wixout", FileFormat.Wixout },
@@ -36,7 +39,7 @@ namespace WixToolset.Data
36 /// <summary> 39 /// <summary>
37 /// Count of embedded files in the file structure. 40 /// Count of embedded files in the file structure.
38 /// </summary> 41 /// </summary>
39 public int EmbeddedFileCount { get { return this.embeddedFileSizes.Length; } } 42 public int EmbeddedFileCount => this.embeddedFileSizes.Length;
40 43
41 /// <summary> 44 /// <summary>
42 /// File format of the file structure. 45 /// File format of the file structure.
@@ -50,15 +53,15 @@ namespace WixToolset.Data
50 /// <param name="fileFormat">File format for the file structure.</param> 53 /// <param name="fileFormat">File format for the file structure.</param>
51 /// <param name="embedFilePaths">Paths to files to embedd in the file structure.</param> 54 /// <param name="embedFilePaths">Paths to files to embedd in the file structure.</param>
52 /// <returns>Newly created file structure.</returns> 55 /// <returns>Newly created file structure.</returns>
53 public static FileStructure Create(Stream stream, FileFormat fileFormat, List<string> embedFilePaths) 56 public static FileStructure Create(Stream stream, FileFormat fileFormat, IEnumerable<string> embedFilePaths)
54 { 57 {
55 FileStructure fs = new FileStructure(); 58 var fs = new FileStructure();
56 using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) 59
57 using (BinaryWriter writer = new BinaryWriter(wrapper)) 60 using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
58 { 61 {
59 fs.WriteType(writer, fileFormat); 62 fs.WriteType(writer, fileFormat);
60 63
61 fs.WriteEmbeddedFiles(writer, embedFilePaths ?? new List<string>()); 64 fs.WriteEmbeddedFiles(writer, embedFilePaths.ToArray());
62 65
63 // Remember the data stream offset, which is right after the embedded files have been written. 66 // Remember the data stream offset, which is right after the embedded files have been written.
64 fs.dataStreamOffset = stream.Position; 67 fs.dataStreamOffset = stream.Position;
@@ -76,13 +79,13 @@ namespace WixToolset.Data
76 /// <returns>File structure populated from the stream.</returns> 79 /// <returns>File structure populated from the stream.</returns>
77 public static FileStructure Read(Stream stream) 80 public static FileStructure Read(Stream stream)
78 { 81 {
79 FileStructure fs = new FileStructure(); 82 var fs = new FileStructure();
80 using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) 83
81 using (BinaryReader reader = new BinaryReader(wrapper)) 84 using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
82 { 85 {
83 fs.FileFormat = FileStructure.ReadFileFormat(reader); 86 fs.FileFormat = FileStructure.ReadFileFormat(reader);
84 87
85 if (FileFormat.Unknown != fs.FileFormat) 88 if (fs.FileFormat != FileFormat.Unknown)
86 { 89 {
87 fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader); 90 fs.embeddedFileSizes = FileStructure.ReadEmbeddedFileSizes(reader);
88 91
@@ -107,8 +110,7 @@ namespace WixToolset.Data
107 /// <returns>Best guess at file format.</returns> 110 /// <returns>Best guess at file format.</returns>
108 public static FileFormat GuessFileFormatFromExtension(string extension) 111 public static FileFormat GuessFileFormatFromExtension(string extension)
109 { 112 {
110 FileFormat format; 113 return FileStructure.SupportedFileFormats.TryGetValue(extension.TrimStart('.').ToLowerInvariant(), out var format) ? format : FileFormat.Unknown;
111 return FileStructure.SupportedFileFormats.TryGetValue(extension.TrimStart('.').ToLowerInvariant(), out format) ? format : FileFormat.Unknown;
112 } 114 }
113 115
114 /// <summary> 116 /// <summary>
@@ -124,8 +126,7 @@ namespace WixToolset.Data
124 126
125 try 127 try
126 { 128 {
127 using (NonClosingStreamWrapper wrapper = new NonClosingStreamWrapper(stream)) 129 using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
128 using (BinaryReader reader = new BinaryReader(wrapper))
129 { 130 {
130 format = FileStructure.ReadFileFormat(reader); 131 format = FileStructure.ReadFileFormat(reader);
131 } 132 }
@@ -182,7 +183,21 @@ namespace WixToolset.Data
182 } 183 }
183 184
184 /// <summary> 185 /// <summary>
185 /// Disposes of the internsl state of the file structure. 186 /// Gets the data of the file as a string.
187 /// </summary>
188 /// <returns>String contents data of the file.</returns>
189 public string GetData()
190 {
191 var bytes = new byte[this.stream.Length - this.dataStreamOffset];
192
193 this.stream.Seek(this.dataStreamOffset, SeekOrigin.Begin);
194 this.stream.Read(bytes, 0, bytes.Length);
195
196 return Encoding.UTF8.GetString(bytes);
197 }
198
199 /// <summary>
200 /// Disposes of the internal state of the file structure.
186 /// </summary> 201 /// </summary>
187 public void Dispose() 202 public void Dispose()
188 { 203 {
@@ -217,7 +232,13 @@ namespace WixToolset.Data
217 { 232 {
218 FileFormat format = FileFormat.Unknown; 233 FileFormat format = FileFormat.Unknown;
219 234
220 string type = new string(reader.ReadChars(6)); 235 string type = new string(reader.ReadChars(3));
236 if (FileStructure.SupportedFileFormats.TryGetValue(type, out format))
237 {
238 return format;
239 }
240
241 type += new string(reader.ReadChars(3));
221 FileStructure.SupportedFileFormats.TryGetValue(type, out format); 242 FileStructure.SupportedFileFormats.TryGetValue(type, out format);
222 243
223 return format; 244 return format;
@@ -256,21 +277,21 @@ namespace WixToolset.Data
256 277
257 this.FileFormat = fileFormat; 278 this.FileFormat = fileFormat;
258 279
259 Debug.Assert(6 == type.ToCharArray().Length); 280 Debug.Assert(3 == type.ToCharArray().Length || 6 == type.ToCharArray().Length);
260 writer.Write(type.ToCharArray()); 281 writer.Write(type.ToCharArray());
261 return writer; 282 return writer;
262 } 283 }
263 284
264 private BinaryWriter WriteEmbeddedFiles(BinaryWriter writer, List<string> embedFilePaths) 285 private BinaryWriter WriteEmbeddedFiles(BinaryWriter writer, string[] embedFilePaths)
265 { 286 {
266 // First write the count of embedded files as a Uint32; 287 // First write the count of embedded files as a Uint32;
267 writer.Write((uint)embedFilePaths.Count); 288 writer.Write((uint)embedFilePaths.Length);
268 289
269 this.embeddedFileSizes = new long[embedFilePaths.Count]; 290 this.embeddedFileSizes = new long[embedFilePaths.Length];
270 291
271 // Next write out the size of each file as a Uint64 in order. 292 // Next write out the size of each file as a Uint64 in order.
272 FileInfo[] files = new FileInfo[embedFilePaths.Count]; 293 FileInfo[] files = new FileInfo[embedFilePaths.Length];
273 for (int i = 0; i < embedFilePaths.Count; ++i) 294 for (int i = 0; i < embedFilePaths.Length; ++i)
274 { 295 {
275 files[i] = new FileInfo(embedFilePaths[i]); 296 files[i] = new FileInfo(embedFilePaths[i]);
276 297