-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderArchive.m
211 lines (166 loc) · 4.83 KB
/
FolderArchive.m
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
/* Copyright (c) 2010 Per Johansson, per at morth.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "FolderArchive.h"
static NSArray *resourceKeys;
@implementation FolderArchiveMember
- (id)initWithWrapper:(FolderArchive*)w URL:(NSURL*)u level:(NSUInteger)l encoding:(NSStringEncoding)enc error:(NSError**)error
{
self = [super init];
if (self)
{
wrapper = w;
encoding = enc;
url = u;
dataAvailable = YES;
data = nil;
resources = [url resourceValuesForKeys:resourceKeys error:error];
if (!resources)
return nil;
}
return self;
}
- (BOOL)pathnameAvailable
{
if (!url)
return [super pathnameAvailable];
return YES;
}
- (NSString*)pathname
{
if (!url)
return [super pathname];
NSArray *comps = [url pathComponents];
NSRange r = {[comps count] - level - 1, level + 1};
return [NSString pathWithComponents:[comps subarrayWithRange:r]];
}
- (const char*)cPathname
{
if (!url)
return [super cPathname];
return [[self pathname] cStringUsingEncoding:encoding];
}
- (BOOL)sizeAvailable
{
if (!url)
return [super sizeAvailable];
return [[resources objectForKey:NSURLIsRegularFileKey] boolValue];
}
- (int64_t)size
{
if (!url)
return [super size];
if (![[resources objectForKey:NSURLIsRegularFileKey] boolValue])
[NSException raise:ArchiveMemberInfoNotAvailableException format:@"size is only available for regular files"];
return [[resources objectForKey:NSURLFileSizeKey] longLongValue];
}
- (BOOL)fetchDataWithError:(NSError **)error
{
if (!url)
return [super fetchDataWithError:error];
if (data)
{
if (error)
*error = nil;
return YES;
}
if (!dataAvailable)
{
if (error)
*error = nil;
return NO;
}
[self willChangeValueForKey:@"data"];
data = [NSData dataWithContentsOfURL:url options:NSDataReadingMapped error:error];
[self didChangeValueForKey:@"data"];
if (wrapper)
wrapper.uncompressedOffset += [data length];
return data != nil;
}
- (BOOL)skipDataWithError:(NSError **)error
{
if (!url)
return [super skipDataWithError:error];
if (data)
{
if (error)
*error = nil;
return NO;
}
[self willChangeValueForKey:@"dataAvailable"];
dataAvailable = NO;
[self didChangeValueForKey:@"dataAvailable"];
if (wrapper)
wrapper.uncompressedOffset += [[resources objectForKey:NSURLFileSizeKey] longLongValue];
return YES;
}
- (BOOL)extractToURL:(NSURL *)dst createDirectories:(BOOL)create error:(NSError **)error
{
if (url && !data && dataAvailable)
{
if (![self fetchDataWithError:error])
return NO;
}
return [super extractToURL:dst createDirectories:create error:error];
}
@end
@implementation FolderArchive
+ (void)initialize
{
if (!resourceKeys)
resourceKeys = [NSArray arrayWithObjects:
NSURLIsRegularFileKey,
NSURLFileSizeKey,
nil];
}
- (id)initForReadingFromURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
{
NSNumber *isd;
if ([url getResourceValue:&isd forKey:NSURLIsDirectoryKey error:nil] && [isd boolValue])
{
self = [self init];
if (self)
{
errPtr = nil;
enumerator = [[NSFileManager defaultManager] enumeratorAtURL:url
includingPropertiesForKeys:resourceKeys
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:^(NSURL *u, NSError *err) { if (errPtr) *errPtr = err; return NO; }];
encoding = enc;
}
}
else
self = [super initForReadingFromURL:url encoding:enc error:error];
isDir = isd;
return self;
}
- (ArchiveMember *)nextMemberWithError:(NSError**)error
{
if (!isDir || ![isDir boolValue])
return [super nextMemberWithError:error];
errPtr = error;
NSURL *url = [enumerator nextObject];
if (!url)
return nil;
if (![enumerator level])
return [self nextMemberWithError:error];
return [[[self memberClass] alloc] initWithWrapper:self URL:url level:[enumerator level] encoding:encoding error:error];
}
@end