aboutsummaryrefslogtreecommitdiff
path: root/src/tools/Dtf/XPack/XPack.cs
blob: cc52bb7de17c4813ff47f96a44956d82b0974b62 (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
// 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.Dtf.Tools.XPack
{
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Text;
    using WixToolset.Dtf.Compression;

    public class XPack
    {
        public static void Usage(TextWriter writer)
        {
            writer.WriteLine("Usage: XPack /P <archive.cab> <directory>");
            writer.WriteLine("Usage: XPack /P <archive.zip> <directory>");
            writer.WriteLine();
            writer.WriteLine("Packs all files in a directory tree into an archive,");
            writer.WriteLine("using either the cab or zip format. Any existing archive");
            writer.WriteLine("with the same name will be overwritten.");
            writer.WriteLine();
            writer.WriteLine("Usage: XPack /U <archive.cab> <directory>");
            writer.WriteLine("Usage: XPack /U <archive.zip> <directory>");
            writer.WriteLine();
            writer.WriteLine("Unpacks all files from a cab or zip archive to the");
            writer.WriteLine("specified directory. Any existing files with the same");
            writer.WriteLine("names will be overwritten.");
        }

        public static void Main(string[] args)
        {
            try
            {
                if (args.Length == 3 && args[0].ToUpperInvariant() == "/P")
                {
                    ArchiveInfo a = GetArchive(args[1]);
                    a.Pack(args[2], true, CompressionLevel.Max, ProgressHandler);
                }
                else if (args.Length == 3 && args[0].ToUpperInvariant() == "/U")
                {
                    ArchiveInfo a = GetArchive(args[1]);
                    a.Unpack(args[2], ProgressHandler);
                }
                else
                {
                    Usage(Console.Out);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private static void ProgressHandler(object source, ArchiveProgressEventArgs e)
        {
            if (e.ProgressType == ArchiveProgressType.StartFile)
            {
                Console.WriteLine(e.CurrentFileName);
            }
        }

        private static ArchiveInfo GetArchive(string name)
        {
            string extension = Path.GetExtension(name).ToUpperInvariant();
            if (extension == ".CAB")
            {
                return new WixToolset.Dtf.Compression.Cab.CabInfo(name);
            }
            else if (extension == ".ZIP")
            {
                return new WixToolset.Dtf.Compression.Zip.ZipInfo(name);
            }
            else
            {
                throw new ArgumentException("Unknown archive file extension: " + extension);
            }
        }
    }
}