|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | + |
| 4 | +using ICSharpCode.SharpZipLib.Zip; |
| 5 | +using ICSharpCode.SharpZipLib.Core; |
| 6 | + |
| 7 | +namespace Samples.FastZipDemo |
| 8 | +{ |
| 9 | + class MainClass |
| 10 | + { |
| 11 | + enum Operation |
| 12 | + { |
| 13 | + Unknown, |
| 14 | + Create, |
| 15 | + Extract, |
| 16 | + List, |
| 17 | + Error |
| 18 | + }; |
| 19 | + |
| 20 | + void ListZipFile(string fileName, string fileFilter, string directoryFilter) |
| 21 | + { |
| 22 | + ZipFile zipFile = new ZipFile(fileName); |
| 23 | + NameFilter localFileFilter = new NameFilter(fileFilter); |
| 24 | + NameFilter localDirFilter = new NameFilter(directoryFilter); |
| 25 | + if ( zipFile.Size == 0 ) { |
| 26 | + Console.WriteLine("No entries to list"); |
| 27 | + } |
| 28 | + else { |
| 29 | + for ( int i = 0 ; i < zipFile.Size; ++i) |
| 30 | + { |
| 31 | + ZipEntry e = zipFile[i]; |
| 32 | + if ( e.IsFile ) { |
| 33 | + string path = Path.GetDirectoryName(e.Name); |
| 34 | + if ( localDirFilter.IsMatch(path) ) { |
| 35 | + if ( localFileFilter.IsMatch(Path.GetFileName(e.Name)) ) { |
| 36 | + Console.WriteLine(e.Name); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + else if ( e.IsDirectory ) { |
| 41 | + if ( localDirFilter.IsMatch(e.Name) ) { |
| 42 | + Console.WriteLine(e.Name); |
| 43 | + } |
| 44 | + } |
| 45 | + else { |
| 46 | + Console.WriteLine(e.Name); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + void ListFile(object sender, ScanEventArgs e) |
| 53 | + { |
| 54 | + Console.WriteLine("{0}", e.Name); |
| 55 | + } |
| 56 | + |
| 57 | + void ListDir(object Sender, DirectoryEventArgs e) |
| 58 | + { |
| 59 | + if ( !e.HasMatchingFiles ) { |
| 60 | + Console.WriteLine("Dir:{0}", e.Name); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter) |
| 65 | + { |
| 66 | + FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter); |
| 67 | + scanner.ProcessDirectory += new ProcessDirectoryDelegate(ListDir); |
| 68 | + scanner.ProcessFile += new ProcessFileDelegate(ListFile); |
| 69 | + scanner.Scan(directory, recurse); |
| 70 | + } |
| 71 | + |
| 72 | + void ProcessFile(object sender, ScanEventArgs e) |
| 73 | + { |
| 74 | + Console.WriteLine(e.Name); |
| 75 | + } |
| 76 | + |
| 77 | + void ProcessDirectory(object sender, DirectoryEventArgs e) |
| 78 | + { |
| 79 | + if ( !e.HasMatchingFiles ) { |
| 80 | + Console.WriteLine(e.Name); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + void Run(string[] args) |
| 85 | + { |
| 86 | + bool recurse = false; |
| 87 | + string arg1 = null; |
| 88 | + string arg2 = null; |
| 89 | + string fileFilter = null; |
| 90 | + string dirFilter = null; |
| 91 | + bool verbose = false; |
| 92 | + |
| 93 | + bool createEmptyDirs = false; |
| 94 | + |
| 95 | + Operation op = Operation.Unknown; |
| 96 | + int argCount = 0; |
| 97 | + |
| 98 | + for ( int i = 0; i < args.Length; ++i ) { |
| 99 | + if ( args[i][0] == '-' ) { |
| 100 | + string option = args[i].Substring(1).ToLower(); |
| 101 | + string optArg = ""; |
| 102 | + |
| 103 | + int parameterIndex = option.IndexOf('='); |
| 104 | + |
| 105 | + if (parameterIndex >= 0) |
| 106 | + { |
| 107 | + if (parameterIndex < option.Length - 1) { |
| 108 | + optArg = option.Substring(parameterIndex + 1); |
| 109 | + } |
| 110 | + option = option.Substring(0, parameterIndex); |
| 111 | + } |
| 112 | + |
| 113 | + switch ( option ) { |
| 114 | + case "e": |
| 115 | + case "empty": |
| 116 | + createEmptyDirs = true; |
| 117 | + break; |
| 118 | + |
| 119 | + case "x": |
| 120 | + case "extract": |
| 121 | + if ( op == Operation.Unknown ) { |
| 122 | + op = Operation.Extract; |
| 123 | + } |
| 124 | + else { |
| 125 | + Console.WriteLine("Only one operation at a time is permitted"); |
| 126 | + op = Operation.Error; |
| 127 | + } |
| 128 | + break; |
| 129 | + |
| 130 | + case "c": |
| 131 | + case "create": |
| 132 | + if ( op == Operation.Unknown ) { |
| 133 | + op = Operation.Create; |
| 134 | + } |
| 135 | + else { |
| 136 | + Console.WriteLine("Only one operation at a time is permitted"); |
| 137 | + op = Operation.Error; |
| 138 | + } |
| 139 | + break; |
| 140 | + |
| 141 | + case "l": |
| 142 | + case "list": |
| 143 | + if ( op == Operation.Unknown ) { |
| 144 | + op = Operation.List; |
| 145 | + } |
| 146 | + else { |
| 147 | + Console.WriteLine("Only one operation at a time is permitted"); |
| 148 | + op = Operation.Error; |
| 149 | + } |
| 150 | + break; |
| 151 | + |
| 152 | + |
| 153 | + case "r": |
| 154 | + case "recurse": |
| 155 | + recurse = true; |
| 156 | + break; |
| 157 | + |
| 158 | + case "v": |
| 159 | + case "verbose": |
| 160 | + verbose = true; |
| 161 | + break; |
| 162 | + |
| 163 | + case "file": |
| 164 | + fileFilter = optArg; |
| 165 | + break; |
| 166 | + |
| 167 | + case "dir": |
| 168 | + dirFilter = optArg; |
| 169 | + break; |
| 170 | + |
| 171 | + default: |
| 172 | + Console.WriteLine("Unknown option {0}", args[i]); |
| 173 | + op = Operation.Error; |
| 174 | + break; |
| 175 | + } |
| 176 | + } |
| 177 | + else if ( arg1 == null ) { |
| 178 | + arg1 = args[i]; |
| 179 | + ++argCount; |
| 180 | + } |
| 181 | + else if ( arg2 == null ) { |
| 182 | + arg2 = args[i]; |
| 183 | + ++argCount; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + FastZipEvents events = null; |
| 188 | + |
| 189 | + if ( verbose ) { |
| 190 | + events = new FastZipEvents(); |
| 191 | + events.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory); |
| 192 | + events.ProcessFile = new ProcessFileDelegate(ProcessFile); |
| 193 | + } |
| 194 | + |
| 195 | + FastZip sz = new FastZip(events); |
| 196 | + sz.CreateEmptyDirectories = createEmptyDirs; |
| 197 | + switch ( op ) { |
| 198 | + case Operation.Create: |
| 199 | + if ( argCount == 2 ) { |
| 200 | + Console.WriteLine("Creating Zip"); |
| 201 | + |
| 202 | + sz.CreateZip(arg1, arg2, recurse, fileFilter); |
| 203 | + } |
| 204 | + else |
| 205 | + Console.WriteLine("Invalid arguments"); |
| 206 | + break; |
| 207 | + |
| 208 | + case Operation.Extract: |
| 209 | + if ( argCount == 2 ) { |
| 210 | + Console.WriteLine("Extracting Zip"); |
| 211 | + sz.ExtractZip(arg1, arg2, fileFilter); |
| 212 | + } |
| 213 | + else |
| 214 | + Console.WriteLine("Invalid arguments"); |
| 215 | + break; |
| 216 | + |
| 217 | + case Operation.List: |
| 218 | + if ( File.Exists(arg1) ) { |
| 219 | + ListZipFile(arg1, fileFilter, dirFilter); |
| 220 | + } |
| 221 | + else if ( Directory.Exists(arg1) ) { |
| 222 | + ListFileSystem(arg1, recurse, fileFilter, dirFilter); |
| 223 | + } |
| 224 | + else { |
| 225 | + Console.WriteLine("No valid list file or directory"); |
| 226 | + } |
| 227 | + break; |
| 228 | + |
| 229 | + case Operation.Unknown: |
| 230 | + Console.WriteLine( |
| 231 | + "FastZip v0.1\n" |
| 232 | + + " Usage: FastZip {options} operation args\n" |
| 233 | + + "Operation Options: (only one permitted)\n" |
| 234 | + + " -x zipfile targetdir : Extract files from Zip\n" |
| 235 | + + " -c zipfile sourcedir : Create zip file\n" |
| 236 | + + " -l zipfile|dir : List elements\n" |
| 237 | + + "\n" |
| 238 | + + "Behavioural options:\n" |
| 239 | + + " -file={fileFilter}\n" |
| 240 | + + " -dir={dirFilter}\n" |
| 241 | + + " -e Process empty directories\n" |
| 242 | + + " -r Recurse directories\n" |
| 243 | + + " -v Verbose output\n" |
| 244 | + ); |
| 245 | + break; |
| 246 | + |
| 247 | + case Operation.Error: |
| 248 | + // Do nothing for now... |
| 249 | + break; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + public static void Main(string[] args) |
| 254 | + { |
| 255 | + MainClass main = new MainClass(); |
| 256 | + main.Run(args); |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments