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
|
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
|
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
+
-
+
+
-
|
#import "autorelease.h"
#import "macros.h"
#define BUFFER_SIZE 4096
@interface OFZIP: OFObject
- (void)extractFiles: (OFArray*)files
fromArchive: (OFZIPArchive*)archive;
fromArchive: (OFZIPArchive*)archive
override: (int_fast8_t)override;
@end
OF_APPLICATION_DELEGATE(OFZIP)
static void
help(OFStream *stream, int status)
help(OFStream *stream, bool full, int status)
{
[stream writeFormat: @"Usage: %@ -x archive1.zip [file1 file2 ...]\n",
[OFApplication programName]];
[stream writeFormat:
@"Usage: %@ -[fnx] archive1.zip [file1 file2 ...]\n",
[OFApplication programName]];
if (full) {
[stream writeString: @"\nOptions:\n"
@" -f Force / override files\n"
@" -h Show this help\n"
@" -n Never override files\n"
@" -x Extract files\n"];
}
[OFApplication terminateWithStatus: status];
}
@implementation OFZIP
- (void)applicationDidFinishLaunching
{
OFOptionsParser *optionsParser =
[OFOptionsParser parserWithOptions: @"xh"];
[OFOptionsParser parserWithOptions: @"fhnx"];
of_unichar_t option, mode = '\0';
int_fast8_t override = 0;
OFArray *remainingArguments;
void *pool;
OFZIPArchive *archive;
OFArray *files;
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'f':
override = 1;
break;
case 'n':
override = -1;
break;
case 'x':
if (mode != '\0')
help(of_stdout, 1);
help(of_stdout, false, 1);
mode = option;
break;
case 'h':
help(of_stdout, 0);
help(of_stdout, true, 0);
break;
default:
[of_stderr writeFormat: @"%@: Unknown option: -%c\n",
[OFApplication programName],
[optionsParser lastOption]];
[OFApplication terminateWithStatus: 1];
}
}
remainingArguments = [optionsParser remainingArguments];
switch (mode) {
case 'x':
pool = objc_autoreleasePoolPush();
if ([remainingArguments count] < 1)
help(of_stderr, 1);
help(of_stderr, false, 1);
files = [remainingArguments objectsInRange:
of_range(1, [remainingArguments count] - 1)];
archive = [OFZIPArchive archiveWithPath:
[remainingArguments firstObject]];
[self extractFiles: files
fromArchive: archive];
fromArchive: archive
override: override];
objc_autoreleasePoolPop(pool);
break;
default:
help(of_stderr, 1);
help(of_stderr, true, 1);
break;
}
[OFApplication terminate];
}
- (void)extractFiles: (OFArray*)files
fromArchive: (OFZIPArchive*)archive
override: (int_fast8_t)override
{
OFEnumerator *enumerator = [[archive entries] objectEnumerator];
OFZIPArchiveEntry *entry;
int_fast8_t override = 0;
bool all = ([files count] == 0);
OFMutableSet *missing = [OFMutableSet setWithArray: files];
while ((entry = [enumerator nextObject]) != nil) {
void *pool = objc_autoreleasePoolPush();
OFString *fileName = [entry fileName];
OFString *outFileName = [fileName stringByStandardizingPath];
|