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
|
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
|
+
+
+
-
+
+
+
-
+
+
-
+
+
+
+
-
-
-
-
-
-
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#import "OFApplication.h"
#import "OFArray.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFOptionsParser.h"
#import "OFSet.h"
#import "OFStdIOStream.h"
#import "OFZIPArchive.h"
#import "OFZIPArchiveEntry.h"
#import "autorelease.h"
#import "macros.h"
#define BUFFER_SIZE 4096
@interface OFZIP: OFObject
- (void)listFilesInArchive: (OFZIPArchive*)archive
verbose: (bool)verbose;
- (void)extractFiles: (OFArray*)files
fromArchive: (OFZIPArchive*)archive
override: (int_fast8_t)override;
@end
OF_APPLICATION_DELEGATE(OFZIP)
static void
help(OFStream *stream, bool full, int status)
{
[stream writeFormat:
@"Usage: %@ -[fnx] archive1.zip [file1 file2 ...]\n",
@"Usage: %@ -[flnvx] archive.zip [file1 file2 ...]\n",
[OFApplication programName]];
if (full) {
[stream writeString: @"\nOptions:\n"
@" -f Force / override files\n"
@" -h Show this help\n"
@" -l List all files in the archive\n"
@" -n Never override files\n"
@" -v Verbose output for file list\n"
@" -x Extract files\n"];
}
[OFApplication terminateWithStatus: status];
}
@implementation OFZIP
- (void)applicationDidFinishLaunching
{
OFOptionsParser *optionsParser =
[OFOptionsParser parserWithOptions: @"fhnx"];
[OFOptionsParser parserWithOptions: @"fhlnvx"];
of_unichar_t option, mode = '\0';
int_fast8_t override = 0;
bool verbose = false;
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 'v':
verbose = true;
break;
case 'l':
case 'x':
if (mode != '\0')
help(of_stdout, false, 1);
mode = option;
break;
case 'h':
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, false, 1);
if ([remainingArguments count] < 1)
help(of_stderr, false, 1);
files = [remainingArguments objectsInRange:
of_range(1, [remainingArguments count] - 1)];
archive = [OFZIPArchive archiveWithPath:
[remainingArguments firstObject]];
files = [remainingArguments objectsInRange:
of_range(1, [remainingArguments count] - 1)];
archive = [OFZIPArchive archiveWithPath:
[remainingArguments firstObject]];
switch (mode) {
case 'l':
if ([files count] > 0)
help(of_stderr, false, 1);
[self listFilesInArchive: archive
verbose: verbose];
break;
case 'x':
[self extractFiles: files
fromArchive: archive
override: override];
objc_autoreleasePoolPop(pool);
break;
default:
help(of_stderr, true, 1);
break;
}
[OFApplication terminate];
}
- (void)listFilesInArchive: (OFZIPArchive*)archive
verbose: (bool)verbose
{
OFEnumerator *enumerator = [[archive entries] objectEnumerator];
OFZIPArchiveEntry *entry;
while ((entry = [enumerator nextObject]) != nil) {
void *pool = objc_autoreleasePoolPush();
if (verbose) {
OFString *date = [[entry modificationDate]
localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"];
[of_stdout writeFormat:
@"%@: %ju (%ju) bytes; %08X; %@; %@\n",
[entry fileName], [entry uncompressedSize],
[entry compressedSize], [entry CRC32], date,
[entry fileComment]];
} else
[of_stdout writeLine: [entry fileName]];
objc_autoreleasePoolPop(pool);
}
}
- (void)extractFiles: (OFArray*)files
fromArchive: (OFZIPArchive*)archive
override: (int_fast8_t)override
{
OFEnumerator *enumerator = [[archive entries] objectEnumerator];
OFZIPArchiveEntry *entry;
|