|
| 1 | +// SharpZipLib samples |
| 2 | +// Copyright © 2000-2016 AlphaSierraPapa for the SharpZipLib Team |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without modification, are |
| 6 | +// permitted provided that the following conditions are met: |
| 7 | +// |
| 8 | +// - Redistributions of source code must retain the above copyright notice, this list |
| 9 | +// of conditions and the following disclaimer. |
| 10 | +// |
| 11 | +// - Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | +// of conditions and the following disclaimer in the documentation and/or other materials |
| 13 | +// provided with the distribution. |
| 14 | +// |
| 15 | +// - Neither the name of the SharpDevelop team nor the names of its contributors may be used to |
| 16 | +// endorse or promote products derived from this software without specific prior written |
| 17 | +// permission. |
| 18 | +// |
| 19 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS |
| 20 | +// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 21 | +// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 25 | +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 26 | +// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +using System; |
| 29 | +using System.IO; |
| 30 | + |
| 31 | +using ICSharpCode.SharpZipLib.BZip2; |
| 32 | + |
| 33 | +class Cmd_BZip2 |
| 34 | +{ |
| 35 | + static void ShowHelp() |
| 36 | + { |
| 37 | + Console.Error.WriteLine("bzip2, a block-sorting file compressor."); |
| 38 | + Console.Error.WriteLine("Version {0} using SharpZipLib {1}", |
| 39 | + typeof(Cmd_BZip2).Assembly.GetName().Version, |
| 40 | + typeof(BZip2).Assembly.GetName().Version); |
| 41 | + Console.Error.WriteLine("\n usage: {0} [flags and input files in any order]\n", |
| 42 | + // Environment.GetCommandLineArgs()[0] |
| 43 | + System.AppDomain.CurrentDomain.FriendlyName); |
| 44 | + Console.Error.WriteLine(""); |
| 45 | + Console.Error.WriteLine(" -h --help print this message"); |
| 46 | + Console.Error.WriteLine(" -d --decompress force decompression"); |
| 47 | + Console.Error.WriteLine(" -z --compress force compression"); |
| 48 | + Console.Error.WriteLine(" -1 .. -9 set block size to 100k .. 900k"); |
| 49 | + Console.Error.WriteLine(" --fast alias for -1"); |
| 50 | + Console.Error.WriteLine(" --best alias for -9"); |
| 51 | + } |
| 52 | + |
| 53 | + #region Command parsing |
| 54 | + enum Command |
| 55 | + { |
| 56 | + Nothing, |
| 57 | + Help, |
| 58 | + Compress, |
| 59 | + Decompress, |
| 60 | + Stop, |
| 61 | + } |
| 62 | + |
| 63 | + class ArgumentParser |
| 64 | + { |
| 65 | + public ArgumentParser(string[] args) |
| 66 | + { |
| 67 | + if (System.AppDomain.CurrentDomain.FriendlyName.Contains("bzip2")) { |
| 68 | + SetCommand(Command.Compress); |
| 69 | + } else if (System.AppDomain.CurrentDomain.FriendlyName.Contains("bunzip2")) { |
| 70 | + SetCommand(Command.Decompress); |
| 71 | + } |
| 72 | + |
| 73 | + foreach (string argument in args) { |
| 74 | + switch (argument) { |
| 75 | + case "-?": // for backwards compatibility |
| 76 | + case "-h": |
| 77 | + case "--help": |
| 78 | + SetCommand(Command.Help); |
| 79 | + break; |
| 80 | + case "-d": |
| 81 | + case "--decompress": |
| 82 | + SetCommand(Command.Decompress); |
| 83 | + break; |
| 84 | + case "-c": // for backwards compatibility |
| 85 | + case "-z": |
| 86 | + case "--compress": |
| 87 | + SetCommand(Command.Compress); |
| 88 | + break; |
| 89 | + case "-1": |
| 90 | + case "-2": |
| 91 | + case "-3": |
| 92 | + case "-4": |
| 93 | + case "-5": |
| 94 | + case "-6": |
| 95 | + case "-7": |
| 96 | + case "-8": |
| 97 | + case "-9": |
| 98 | + SetLevel((int)argument[1] - 48); |
| 99 | + break; |
| 100 | + case "--fast": |
| 101 | + SetLevel(1); |
| 102 | + break; |
| 103 | + case "--best": |
| 104 | + SetLevel(9); |
| 105 | + break; |
| 106 | + default: |
| 107 | + if (argument[0] == '-') { |
| 108 | + Console.Error.WriteLine("Unknown argument {0}", argument); |
| 109 | + command_ = Command.Stop; |
| 110 | + } else if (file_ == null) { |
| 111 | + file_ = argument; |
| 112 | + |
| 113 | + if (!System.IO.File.Exists(file_)) { |
| 114 | + Console.Error.WriteLine("File not found '{0}'", file_); |
| 115 | + command_ = Command.Stop; |
| 116 | + } |
| 117 | + } else { |
| 118 | + Console.Error.WriteLine("File has already been specified"); |
| 119 | + command_ = Command.Stop; |
| 120 | + } |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (command_ == Command.Nothing) { |
| 126 | + if (file_ == null) { |
| 127 | + command_ = Command.Help; |
| 128 | + } else { |
| 129 | + command_ = Command.Compress; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + void SetCommand(Command command) |
| 135 | + { |
| 136 | + if ((command_ != Command.Nothing) && (command_ != Command.Stop)) { |
| 137 | + Console.Error.WriteLine("Command already specified"); |
| 138 | + command_ = Command.Stop; |
| 139 | + } else { |
| 140 | + command_ = command; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + void SetLevel(int level) |
| 145 | + { |
| 146 | + if (level_ != 0) { |
| 147 | + Console.Error.WriteLine("Level already specified"); |
| 148 | + level_ = 0; |
| 149 | + } else { |
| 150 | + level_ = level; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public string Source { |
| 155 | + get { return file_; } |
| 156 | + } |
| 157 | + |
| 158 | + public string Target { |
| 159 | + get { |
| 160 | + string result; |
| 161 | + if (command_ == Command.Compress) { |
| 162 | + result = file_ + ".bz"; |
| 163 | + } else { |
| 164 | + result = Path.GetFileNameWithoutExtension(file_); |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public Command Command { |
| 171 | + get { return command_; } |
| 172 | + } |
| 173 | + |
| 174 | + public int Level { |
| 175 | + get { return level_; } |
| 176 | + } |
| 177 | + |
| 178 | + #region Instance Fields |
| 179 | + Command command_ = Command.Nothing; |
| 180 | + string file_; |
| 181 | + int level_; |
| 182 | + #endregion |
| 183 | + } |
| 184 | + #endregion |
| 185 | + |
| 186 | + public static int Main(string[] args) |
| 187 | + { |
| 188 | + if (args.Length == 0) { |
| 189 | + ShowHelp(); |
| 190 | + return 1; |
| 191 | + } |
| 192 | + |
| 193 | + var parser = new ArgumentParser(args); |
| 194 | + |
| 195 | + switch (parser.Command) { |
| 196 | + case Command.Help: |
| 197 | + ShowHelp(); |
| 198 | + break; |
| 199 | + |
| 200 | + case Command.Compress: |
| 201 | + Console.WriteLine("Compressing {0} to {1} at level {2}", parser.Source, parser.Target, parser.Level); |
| 202 | + BZip2.Compress(File.OpenRead(parser.Source), File.Create(parser.Target), true, parser.Level); |
| 203 | + break; |
| 204 | + |
| 205 | + case Command.Decompress: |
| 206 | + Console.WriteLine("Decompressing {0} to {1}", parser.Source, parser.Target); |
| 207 | + BZip2.Decompress(File.OpenRead(parser.Source), File.Create(parser.Target), true); |
| 208 | + break; |
| 209 | + } |
| 210 | + |
| 211 | + return 0; |
| 212 | + } |
| 213 | +} |
0 commit comments