diff options
Diffstat (limited to 'src/samples/Dtf/XPack/XPack.cs')
-rw-r--r-- | src/samples/Dtf/XPack/XPack.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/samples/Dtf/XPack/XPack.cs b/src/samples/Dtf/XPack/XPack.cs new file mode 100644 index 00000000..36543a73 --- /dev/null +++ b/src/samples/Dtf/XPack/XPack.cs | |||
@@ -0,0 +1,80 @@ | |||
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.Dtf.Samples.XPack | ||
4 | { | ||
5 | using System; | ||
6 | using System.IO; | ||
7 | using System.Collections.Generic; | ||
8 | using System.Text; | ||
9 | using WixToolset.Dtf.Compression; | ||
10 | |||
11 | public class XPack | ||
12 | { | ||
13 | public static void Usage(TextWriter writer) | ||
14 | { | ||
15 | writer.WriteLine("Usage: XPack /P <archive.cab> <directory>"); | ||
16 | writer.WriteLine("Usage: XPack /P <archive.zip> <directory>"); | ||
17 | writer.WriteLine(); | ||
18 | writer.WriteLine("Packs all files in a directory tree into an archive,"); | ||
19 | writer.WriteLine("using either the cab or zip format. Any existing archive"); | ||
20 | writer.WriteLine("with the same name will be overwritten."); | ||
21 | writer.WriteLine(); | ||
22 | writer.WriteLine("Usage: XPack /U <archive.cab> <directory>"); | ||
23 | writer.WriteLine("Usage: XPack /U <archive.zip> <directory>"); | ||
24 | writer.WriteLine(); | ||
25 | writer.WriteLine("Unpacks all files from a cab or zip archive to the"); | ||
26 | writer.WriteLine("specified directory. Any existing files with the same"); | ||
27 | writer.WriteLine("names will be overwritten."); | ||
28 | } | ||
29 | |||
30 | public static void Main(string[] args) | ||
31 | { | ||
32 | try | ||
33 | { | ||
34 | if (args.Length == 3 && args[0].ToUpperInvariant() == "/P") | ||
35 | { | ||
36 | ArchiveInfo a = GetArchive(args[1]); | ||
37 | a.Pack(args[2], true, CompressionLevel.Max, ProgressHandler); | ||
38 | } | ||
39 | else if (args.Length == 3 && args[0].ToUpperInvariant() == "/U") | ||
40 | { | ||
41 | ArchiveInfo a = GetArchive(args[1]); | ||
42 | a.Unpack(args[2], ProgressHandler); | ||
43 | } | ||
44 | else | ||
45 | { | ||
46 | Usage(Console.Out); | ||
47 | } | ||
48 | } | ||
49 | catch (Exception ex) | ||
50 | { | ||
51 | Console.WriteLine(ex); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | private static void ProgressHandler(object source, ArchiveProgressEventArgs e) | ||
56 | { | ||
57 | if (e.ProgressType == ArchiveProgressType.StartFile) | ||
58 | { | ||
59 | Console.WriteLine(e.CurrentFileName); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | private static ArchiveInfo GetArchive(string name) | ||
64 | { | ||
65 | string extension = Path.GetExtension(name).ToUpperInvariant(); | ||
66 | if (extension == ".CAB") | ||
67 | { | ||
68 | return new WixToolset.Dtf.Compression.Cab.CabInfo(name); | ||
69 | } | ||
70 | else if (extension == ".ZIP") | ||
71 | { | ||
72 | return new WixToolset.Dtf.Compression.Zip.ZipInfo(name); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | throw new ArgumentException("Unknown archive file extension: " + extension); | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||