forked from hjzc/ZipKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZKArchive.m
261 lines (227 loc) · 8.66 KB
/
ZKArchive.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
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
//
// ZKArchive.m
// ZipKit
//
// Created by Karl Moskowski on 08/05/09.
//
#import "ZKArchive.h"
#import "NSDictionary+ZKAdditions.h"
#import "NSFileManager+ZKAdditions.h"
#import "ZKCDTrailer.h"
#import "ZKDefs.h"
@interface NSObject (ZipKitDelegate)
- (void) onZKArchiveDidBeginZip:(ZKArchive *) archive;
- (void) onZKArchiveDidBeginUnzip:(ZKArchive *) archive;
- (void) onZKArchive:(ZKArchive *) archive willZipPath:(NSString *) path;
- (void) onZKArchive:(ZKArchive *) archive willUnzipPath:(NSString *) path;
- (void) onZKArchive:(ZKArchive *) archive didUpdateTotalSize:(unsigned long long) size;
- (void) onZKArchive:(ZKArchive *) archive didUpdateTotalCount:(unsigned long long) count;
- (void) onZKArchive:(ZKArchive *) archive didUpdateBytesWritten:(unsigned long long) byteCount;
- (void) onZKArchiveDidEndZip:(ZKArchive *) archive;
- (void) onZKArchiveDidEndUnzip:(ZKArchive *) archive;
- (void) onZKArchiveDidCancel:(ZKArchive *) archive;
- (void) onZKArchiveDidFail:(ZKArchive *) archive;
- (BOOL) zkDelegateWantsSizes;
@end
#pragma mark -
@implementation ZKArchive
#pragma mark -
#pragma mark Utility
+ (BOOL) validArchiveAtPath:(NSString *) path {
// check that the first few bytes of the file are a local file header
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSData *fileHeader = [fileHandle readDataOfLength:4];
[fileHandle closeFile];
UInt32 headerValue;
[fileHeader getBytes:&headerValue];
return (CFSwapInt32LittleToHost(headerValue) == ZKLFHeaderMagicNumber);
}
+ (NSString *) uniquify:(NSString *) path {
// avoid name collisions by adding a sequence number if needed
NSString * uniquePath = [NSString stringWithString:path];
NSString *dir = [path stringByDeletingLastPathComponent];
NSString *fileNameBase = [[path lastPathComponent] stringByDeletingPathExtension];
NSString *ext = [path pathExtension];
NSUInteger i = 2;
NSFileManager *fm = [[NSFileManager new] autorelease];
while ([fm fileExistsAtPath:uniquePath]) {
uniquePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %u", fileNameBase, i++]];
if (ext && [ext length] > 0)
uniquePath = [uniquePath stringByAppendingPathExtension:ext];
}
return uniquePath;
}
- (void) calculateSizeAndItemCount:(NSDictionary *) userInfo {
NSArray *paths = [userInfo objectForKey:ZKPathsKey];
BOOL rfFlag = [[userInfo objectForKey:ZKusingResourceForkKey] boolValue];
unsigned long long size = 0;
unsigned long long count = 0;
NSFileManager *fmgr = [[NSFileManager new] autorelease];
NSDictionary *dict = nil;
for (NSString *path in paths) {
dict = [fmgr zkTotalSizeAndItemCountAtPath:path usingResourceFork:rfFlag];
size += [dict zk_totalFileSize];
count += [dict zk_itemCount];
}
[self performSelectorOnMainThread:@selector(didUpdateTotalSize:)
withObject:[NSNumber numberWithUnsignedLongLong:size] waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(didUpdateTotalCount:)
withObject:[NSNumber numberWithUnsignedLongLong:count] waitUntilDone:NO];
}
- (NSString *) uniqueExpansionDirectoryIn:(NSString *) enclosingFolder {
NSString *expansionDirectory = [enclosingFolder stringByAppendingPathComponent:ZKExpansionDirectoryName];
NSUInteger i = 1;
while ([self.fileManager fileExistsAtPath:expansionDirectory]) {
expansionDirectory = [enclosingFolder stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@ %u", ZKExpansionDirectoryName, i++]];
}
return expansionDirectory;
}
- (void) cleanUpExpansionDirectory:(NSString *) expansionDirectory {
NSString *enclosingFolder = [expansionDirectory stringByDeletingLastPathComponent];
NSArray *dirContents = [self.fileManager contentsOfDirectoryAtPath:expansionDirectory error:nil];
for (NSString *item in dirContents) {
if (![item isEqualToString:ZKMacOSXDirectory]) {
NSString *subPath = [expansionDirectory stringByAppendingPathComponent:item];
NSString *dest = [enclosingFolder stringByAppendingPathComponent:item];
NSUInteger i = 2;
while ([self.fileManager fileExistsAtPath:dest]) {
NSString *ext = [item pathExtension];
dest = [enclosingFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %u",
[item stringByDeletingPathExtension], i++]];
if (ext && [ext length] > 0)
dest = [dest stringByAppendingPathExtension:ext];
}
[self.fileManager moveItemAtPath:subPath toPath:dest error:nil];
}
}
[self.fileManager removeItemAtPath:expansionDirectory error:nil];
}
#pragma mark -
#pragma mark Accessors
- (NSString *) comment {
return self.cdTrailer.comment;
}
- (void) setComment:(NSString *)comment {
self.cdTrailer.comment = comment;
}
#pragma mark -
#pragma mark Delegate
- (void) setInvoker:(id)i {
_invoker = i;
if (_invoker) {
irtsIsCancelled = [self.invoker respondsToSelector:@selector(isCancelled)];
} else {
irtsIsCancelled = NO;
}
}
- (void) setDelegate:(id)d {
_delegate = d;
if (_delegate) {
drtsDelegateWantsSizes = [_delegate respondsToSelector:@selector(zkDelegateWantsSizes)];
drtsDidBeginZip = [_delegate respondsToSelector:@selector(onZKArchiveDidBeginZip:)];
drtsDidBeginUnzip = [_delegate respondsToSelector:@selector(onZKArchiveDidBeginUnzip:)];
drtsWillZipPath = [_delegate respondsToSelector:@selector(onZKArchive:willZipPath:)];
drtsWillUnzipPath = [_delegate respondsToSelector:@selector(onZKArchive:willUnzipPath:)];
drtsDidEndZip = [_delegate respondsToSelector:@selector(onZKArchiveDidEndZip:)];
drtsDidEndUnzip = [_delegate respondsToSelector:@selector(onZKArchiveDidEndUnzip:)];
drtsDidCancel = [_delegate respondsToSelector:@selector(onZKArchiveDidCancel:)];
drtsDidFail = [_delegate respondsToSelector:@selector(onZKArchiveDidFail:)];
drtsDidUpdateTotalSize = [_delegate respondsToSelector:@selector(onZKArchive:didUpdateTotalSize:)];
drtsDidUpdateTotalCount = [_delegate respondsToSelector:@selector(onZKArchive:didUpdateTotalCount:)];
drtsDidUpdateBytesWritten = [_delegate respondsToSelector:@selector(onZKArchive:didUpdateBytesWritten:)];
} else {
drtsDelegateWantsSizes = NO;
drtsDidBeginZip = NO;
drtsDidBeginUnzip = NO;
drtsWillZipPath = NO;
drtsWillUnzipPath = NO;
drtsDidEndZip = NO;
drtsDidEndUnzip = NO;
drtsDidCancel = NO;
drtsDidFail = NO;
drtsDidUpdateTotalSize = NO;
drtsDidUpdateTotalCount = NO;
drtsDidUpdateBytesWritten = NO;
}
}
- (BOOL) delegateWantsSizes {
BOOL delegateWantsSizes = NO;
if (drtsDelegateWantsSizes) {
delegateWantsSizes = [self.delegate zkDelegateWantsSizes];
}
return delegateWantsSizes;
}
- (void) didBeginZip {
if (drtsDidBeginZip)
[self.delegate onZKArchiveDidBeginZip:self];
}
- (void) didBeginUnzip {
if (drtsDidBeginUnzip)
[self.delegate onZKArchiveDidBeginUnzip:self];
}
- (void) willZipPath:(NSString *) path {
if (drtsWillZipPath)
[self.delegate onZKArchive:self willZipPath:path];
}
- (void) willUnzipPath:(NSString *) path {
if (drtsWillUnzipPath)
[self.delegate onZKArchive:self willUnzipPath:path];
}
- (void) didEndZip {
if (drtsDidEndZip)
[self.delegate onZKArchiveDidEndZip:self];
}
- (void) didEndUnzip {
if (drtsDidEndUnzip)
[self.delegate onZKArchiveDidEndUnzip:self];
}
- (void) didCancel {
if (drtsDidCancel)
[self.delegate onZKArchiveDidCancel:self];
}
- (void) didFail {
if (drtsDidFail)
[self.delegate onZKArchiveDidFail:self];
}
- (void) didUpdateTotalSize:(NSNumber *) size {
if (drtsDidUpdateTotalSize)
[self.delegate onZKArchive:self didUpdateTotalSize:[size unsignedLongLongValue]];
}
- (void) didUpdateTotalCount:(NSNumber *) count {
if (drtsDidUpdateTotalCount)
[self.delegate onZKArchive:self didUpdateTotalCount:[count unsignedLongLongValue]];
}
- (void) didUpdateBytesWritten:(NSNumber *) byteCount {
if (drtsDidUpdateBytesWritten)
[self.delegate onZKArchive:self didUpdateBytesWritten:[byteCount unsignedLongLongValue]];
}
#pragma mark -
#pragma mark Setup
- (id) init {
if (self = [super init]) {
self.invoker = nil;
self.delegate = nil;
self.archivePath = nil;
self.centralDirectory = [NSMutableArray array];
self.fileManager = [[NSFileManager new] autorelease];
self.cdTrailer = [[ZKCDTrailer new] autorelease];
self.throttleThreadSleepTime = 0.0;
}
return self;
}
- (void) dealloc {
self.invoker = nil;
self.delegate = nil;
self.archivePath = nil;
self.centralDirectory = nil;
self.fileManager = nil;
self.cdTrailer = nil;
[super dealloc];
}
- (NSString *) description {
return [NSString stringWithFormat: @"%@\n\ttrailer:%@\n\tcentral directory:%@", self.archivePath, self.cdTrailer, self.centralDirectory];
}
@synthesize invoker = _invoker, delegate = _delegate, archivePath = _archivePath, centralDirectory = _centralDirectory, fileManager = _fileManager, cdTrailer = _cdTrailer, throttleThreadSleepTime = _throttleThreadSleepTime;
@dynamic comment;
@end