-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathFastZip.cs
More file actions
335 lines (290 loc) · 8.1 KB
/
Copy pathFastZip.cs
File metadata and controls
335 lines (290 loc) · 8.1 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
class FastZipDemo
{
enum Operation
{
Unknown,
Create,
Extract,
List,
Error
};
static void ListZipFile(string fileName, string fileFilter, string directoryFilter)
{
using (ZipFile zipFile = new ZipFile(fileName)) {
var localFileFilter = new PathFilter(fileFilter);
var localDirFilter = new PathFilter(directoryFilter);
if (zipFile.Count == 0) {
Console.WriteLine("No entries to list");
} else {
for (int i = 0; i < zipFile.Count; ++i) {
ZipEntry e = zipFile[i];
if (e.IsFile) {
string path = Path.GetDirectoryName(e.Name);
if (localDirFilter.IsMatch(path)) {
if (localFileFilter.IsMatch(Path.GetFileName(e.Name))) {
Console.WriteLine(e.Name);
}
}
} else if (e.IsDirectory) {
if (localDirFilter.IsMatch(e.Name)) {
Console.WriteLine(e.Name);
}
} else {
Console.WriteLine(e.Name);
}
}
}
}
}
void ListFile(object sender, ScanEventArgs e)
{
Console.WriteLine("{0}", e.Name);
}
void ListDir(object sender, DirectoryEventArgs e)
{
if (!e.HasMatchingFiles) {
Console.WriteLine("Dir:{0}", e.Name);
}
}
void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
{
var scanner = new FileSystemScanner(fileFilter, directoryFilter);
scanner.ProcessDirectory += ListDir;
scanner.ProcessFile += ListFile;
scanner.Scan(directory, recurse);
}
void ShowProgress(object sender, ProgressEventArgs e)
{
// Very ugly but this is a sample!
Console.WriteLine("{0}%", e.PercentComplete);
}
void ProcessFile(object sender, ScanEventArgs e)
{
Console.WriteLine(e.Name);
}
void ProcessDirectory(object sender, DirectoryEventArgs e)
{
if (!e.HasMatchingFiles) {
Console.WriteLine(e.Name);
}
}
bool ConfirmOverwrite(string file)
{
Console.WriteLine("Overwrite file {0} Y/N", file);
string yesNo = Console.ReadLine();
return (yesNo != null) && string.Compare(yesNo.Trim(), "y", true) == 0;
}
void Run(string[] args)
{
bool recurse = false;
string arg1 = null;
string arg2 = null;
string fileFilter = null;
string dirFilter = null;
bool verbose = false;
bool restoreDates = false;
bool restoreAttributes = false;
bool progress = false;
TimeSpan interval = TimeSpan.FromSeconds(1);
bool createEmptyDirs = false;
FastZip.Overwrite overwrite = FastZip.Overwrite.Always;
FastZip.ConfirmOverwriteDelegate confirmOverwrite = null;
Operation op = Operation.Unknown;
int argCount = 0;
for (int i = 0; i < args.Length; ++i) {
if (args[i][0] == '-') {
string option = args[i].Substring(1).ToLower();
string optArg = "";
int parameterIndex = option.IndexOf('=');
if (parameterIndex >= 0) {
if (parameterIndex < option.Length - 1) {
optArg = option.Substring(parameterIndex + 1);
}
option = option.Substring(0, parameterIndex);
}
switch (option) {
case "e":
case "empty":
createEmptyDirs = true;
break;
case "x":
case "extract":
if (op == Operation.Unknown) {
op = Operation.Extract;
} else {
Console.WriteLine("Only one operation at a time is permitted");
op = Operation.Error;
}
break;
case "c":
case "create":
if (op == Operation.Unknown) {
op = Operation.Create;
} else {
Console.WriteLine("Only one operation at a time is permitted");
op = Operation.Error;
}
break;
case "l":
case "list":
if (op == Operation.Unknown) {
op = Operation.List;
} else {
Console.WriteLine("Only one operation at a time is permitted");
op = Operation.Error;
}
break;
case "p":
case "progress":
progress = true;
verbose = true;
break;
case "r":
case "recurse":
recurse = true;
break;
case "v":
case "verbose":
verbose = true;
break;
case "i":
if (optArg.Length > 0) {
interval = TimeSpan.FromSeconds(int.Parse(optArg));
}
break;
case "file":
if (NameFilter.IsValidFilterExpression(optArg)) {
fileFilter = optArg;
} else {
Console.WriteLine("File filter expression contains an invalid regular expression");
op = Operation.Error;
}
break;
case "dir":
if (NameFilter.IsValidFilterExpression(optArg)) {
dirFilter = optArg;
} else {
Console.WriteLine("Path filter expression contains an invalid regular expression");
op = Operation.Error;
}
break;
case "o":
case "overwrite":
switch (optArg) {
case "always":
overwrite = FastZip.Overwrite.Always;
confirmOverwrite = null;
break;
case "never":
overwrite = FastZip.Overwrite.Never;
confirmOverwrite = null;
break;
case "prompt":
overwrite = FastZip.Overwrite.Prompt;
confirmOverwrite = new FastZip.ConfirmOverwriteDelegate(ConfirmOverwrite);
break;
default:
Console.WriteLine("Invalid overwrite option");
op = Operation.Error;
break;
}
break;
case "oa":
restoreAttributes = true;
break;
case "od":
restoreDates = true;
break;
default:
Console.WriteLine("Unknown option {0}", args[i]);
op = Operation.Error;
break;
}
} else if (arg1 == null) {
arg1 = args[i];
++argCount;
} else if (arg2 == null) {
arg2 = args[i];
++argCount;
}
}
FastZipEvents events = null;
if (verbose) {
events = new FastZipEvents();
//events.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
events.ProcessDirectory += new EventHandler<DirectoryEventArgs>(ProcessDirectory);
events.ProcessFile = new ProcessFileHandler(ProcessFile);
if (progress) {
events.Progress = new ProgressHandler(ShowProgress);
events.ProgressInterval = interval;
}
}
var fastZip = new FastZip(events);
fastZip.CreateEmptyDirectories = createEmptyDirs;
fastZip.RestoreAttributesOnExtract = restoreAttributes;
fastZip.RestoreDateTimeOnExtract = restoreDates;
switch (op) {
case Operation.Create:
if (argCount == 2) {
Console.WriteLine("Creating Zip");
fastZip.CreateZip(arg1, arg2, recurse, fileFilter, dirFilter);
} else
Console.WriteLine("Invalid arguments");
break;
case Operation.Extract:
if (argCount == 2) {
Console.WriteLine("Extracting Zip");
fastZip.ExtractZip(arg1, arg2, overwrite, confirmOverwrite, fileFilter, dirFilter, restoreDates);
} else
Console.WriteLine("zipfile and target directory not specified");
break;
case Operation.List:
if (File.Exists(arg1)) {
ListZipFile(arg1, fileFilter, dirFilter);
} else if (Directory.Exists(arg1)) {
ListFileSystem(arg1, recurse, fileFilter, dirFilter);
} else {
Console.WriteLine("No valid list file or directory");
}
break;
case Operation.Unknown:
Console.WriteLine(
"FastZip v0.5\n"
+ " Usage: FastZip {options} operation args\n"
+ "Operation Options: (only one permitted)\n"
+ " -x zipfile targetdir : Extract files from Zip\n"
+ " -c zipfile sourcedir : Create zip file\n"
+ " -l zipfile|dir : List elements\n"
+ "\n"
+ "Behavioural options:\n"
+ " -dir={dirFilter}\n"
+ " -file={fileFilter}\n"
+ " -e Process empty directories\n"
+ " -i Progress interval in seconds\n"
+ " -p Show file progress\n"
+ " -r Recurse directories\n"
+ " -v Verbose output\n"
+ " -oa Restore file attributes on extract\n"
+ " -ot Restore file date time on extract\n"
+ " -overwrite=prompt|always|never : Overwrite on extract handling\n"
);
break;
case Operation.Error:
// Do nothing for now...
break;
}
}
/// <summary>
/// Main entry point for FastZip sample.
/// </summary>
/// <param name="args">The arguments provided to this process.</param>
public static void Main(string[] args)
{
var main = new FastZipDemo();
main.Run(args);
}
}