1 using System;
2 using System.IO;
3 using System.IO.Compression;
4
5 void PrintHelp()
6 {
7 Console.Out() <<
8 "Usage: deflate [options] <input-file> <output-file>" << endl() <<
9 "options:" << endl() <<
10 "--help (-h)" << endl() <<
11 " Print this help." << endl() <<
12 "--verbose (-v)" << endl() <<
13 " Be verbose." << endl() <<
14 "--compress (-c)" << endl() <<
15 " Compress <input-file> to <output-file>." << endl() <<
16 " (default)" << endl() <<
17 "--extract (-x)" << endl() <<
18 " Extract <input-file> to <output-file>." << endl();
19 ;
20 }
21
22 public enum Mode
23 {
24 compress, extract
25 }
26
27 int main(int argc, const char** argv)
28 {
29 try
30 {
31 string inputFile;
32 string outputFile;
33 int argCount = 0;
34 Mode mode = Mode.compress;
35 bool verbose = false;
36 for (int i = 1; i < argc; ++i;)
37 {
38 string arg = argv[i];
39 if (arg.StartsWith("-"))
40 {
41 if (arg == "--help" || arg == "-h")
42 {
43 PrintHelp();
44 return 0;
45 }
46 else if (arg == "--verbose" || arg == "-v")
47 {
48 verbose = true;
49 }
50 else if (arg == "--compress" || arg == "-c")
51 {
52 mode = Mode.compress;
53 }
54 else if (arg == "--extract" || arg == "-x")
55 {
56 mode = Mode.extract;
57 }
58 else
59 {
60 throw Exception("unknown argument '" + arg + "'");
61 }
62 }
63 else
64 {
65 switch (argCount)
66 {
67 case 0:
68 {
69 inputFile = arg;
70 ++argCount;
71 break;
72 }
73 case 1:
74 {
75 outputFile = arg;
76 ++argCount;
77 break;
78 }
79 default:
80 {
81 ++argCount;
82 break;
83 }
84 }
85 }
86 }
87 if (argCount < 2)
88 {
89 throw Exception("missing arguments (try --help)");
90 }
91 else if (argCount > 2)
92 {
93 throw Exception("extra arguments (try --help)");
94 }
95 if (verbose)
96 {
97 if (mode == Mode.compress)
98 {
99 Console.Out() << "compressing '" << inputFile << "' to '" << outputFile << "'..." << endl();
100 }
101 else if (mode == Mode.extract)
102 {
103 Console.Out() << "extracting '" << inputFile << "' to '" << outputFile << "'..." << endl();
104 }
105 }
106 if (mode == Mode.compress)
107 {
108 FileByteStream in(inputFile, cast<OpenMode>(OpenMode.read | OpenMode.binary));
109 SharedPtr<ByteStream> out(new FileByteStream(outputFile, cast<OpenMode>(OpenMode.write | OpenMode.binary)));
110 DeflateStream compressStream(out, CompressionMode.compress);
111 in.CopyTo(compressStream);
112 }
113 else if (mode == Mode.extract)
114 {
115 SharedPtr<ByteStream> in(new FileByteStream(inputFile, cast<OpenMode>(OpenMode.read | OpenMode.binary)));
116 DeflateStream extractStream(in, CompressionMode.decompress);
117 FileByteStream out(outputFile, cast<OpenMode>(OpenMode.write | OpenMode.binary));
118 extractStream.CopyTo(out);
119 }
120 if (verbose)
121 {
122 if (mode == Mode.compress)
123 {
124 Console.Out() << "compressed." << endl();
125 }
126 else if (mode == Mode.extract)
127 {
128 Console.Out() << "extracted." << endl();
129 }
130 }
131 }
132 catch (const Exception& ex)
133 {
134 Console.Error() << ex.Message() << endl();
135 return 1;
136 }
137 return 0;
138 }