-
Notifications
You must be signed in to change notification settings - Fork 36
/
TiZipArchive.mm
executable file
·353 lines (308 loc) · 8.76 KB
/
TiZipArchive.mm
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// TiZipArchive.mm
//
//
// Created by aish on 08-9-11.
// Copyright 2008 Inc. All rights reserved.
//
#import "TiZipArchive.h"
#import "zlib.h"
#import "zconf.h"
@interface TiZipArchive (Private)
-(void) OutputErrorMessage:(NSString*) msg;
-(BOOL) OverWrite:(NSString*) file;
-(NSDate*) Date1980;
@end
@implementation TiZipArchive
@synthesize delegate = _delegate;
-(id) init
{
if( self=[super init] )
{
_zipFile = NULL ;
}
return self;
}
-(void) dealloc
{
[self CloseZipFile2];
[super dealloc];
}
-(BOOL) CreateZipFile2:(NSString*) zipFile
{
_zipFile = zipOpen( (const char*)[zipFile UTF8String], 0 );
if( !_zipFile )
return NO;
return YES;
}
// Kosso added to set flag for opening existing zip to add files to..
// Uses the flag: 2 instead of 0
-(BOOL) OpenZipFile2:(NSString*) zipFile
{
_zipFile = zipOpen( (const char*)[zipFile UTF8String], 2 );
if( !_zipFile )
return NO;
return YES;
}
-(BOOL) CreateZipFile2:(NSString*) zipFile Password:(NSString*) password
{
_password = password;
return [self CreateZipFile2:zipFile];
}
-(BOOL) addFileToZip:(NSString*) file newname:(NSString*) newname;
{
if( !_zipFile )
return NO;
// tm_zip filetime;
time_t current;
time( ¤t );
zip_fileinfo zipInfo = {0};
// zipInfo.dosDate = (unsigned long) current;
NSError * error;
//NSDictionary* attr = [[NSFileManager defaultManager] fileAttributesAtPath:file traverseLink:YES];
NSDictionary* attr = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&error];
if( attr )
{
NSDate* fileDate = (NSDate*)[attr objectForKey:NSFileModificationDate];
if( fileDate )
{
// some application does use dosDate, but tmz_date instead
// zipInfo.dosDate = [fileDate timeIntervalSinceDate:[self Date1980] ];
NSCalendar* currCalendar = [NSCalendar currentCalendar];
uint flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ;
NSDateComponents* dc = [currCalendar components:flags fromDate:fileDate];
zipInfo.tmz_date.tm_sec = [dc second];
zipInfo.tmz_date.tm_min = [dc minute];
zipInfo.tmz_date.tm_hour = [dc hour];
zipInfo.tmz_date.tm_mday = [dc day];
zipInfo.tmz_date.tm_mon = [dc month] - 1;
zipInfo.tmz_date.tm_year = [dc year];
}
}
int ret ;
NSData* data = nil;
if( [_password length] == 0 )
{
ret = zipOpenNewFileInZip( _zipFile,
(const char*) [newname UTF8String],
&zipInfo,
NULL,0,
NULL,0,
NULL,//comment
Z_DEFLATED,
Z_DEFAULT_COMPRESSION );
}
else
{
data = [ NSData dataWithContentsOfFile:file];
uLong crcValue = crc32( 0L,NULL, 0L );
crcValue = crc32( crcValue, (const Bytef*)[data bytes], [data length] );
ret = zipOpenNewFileInZip3( _zipFile,
(const char*) [newname UTF8String],
&zipInfo,
NULL,0,
NULL,0,
NULL,//comment
Z_DEFLATED,
Z_DEFAULT_COMPRESSION,
0,
15,
8,
Z_DEFAULT_STRATEGY,
[_password cStringUsingEncoding:NSASCIIStringEncoding],
crcValue );
}
if( ret!=Z_OK )
{
return NO;
}
if( data==nil )
{
data = [ NSData dataWithContentsOfFile:file];
}
unsigned int dataLen = [data length];
ret = zipWriteInFileInZip( _zipFile, (const void*)[data bytes], dataLen);
if( ret!=Z_OK )
{
return NO;
}
ret = zipCloseFileInZip( _zipFile );
if( ret!=Z_OK )
return NO;
return YES;
}
-(BOOL) CloseZipFile2
{
_password = nil;
if( _zipFile==NULL )
return NO;
BOOL ret = zipClose( _zipFile,NULL )==Z_OK?YES:NO;
_zipFile = NULL;
return ret;
}
-(BOOL) UnzipOpenFile:(NSString*) zipFile
{
_unzFile = unzOpen( (const char*)[zipFile UTF8String] );
if( _unzFile )
{
unz_global_info globalInfo = {0};
if( unzGetGlobalInfo(_unzFile, &globalInfo )==UNZ_OK )
{
//NSLog([NSString stringWithFormat:@"%d entries in the zip file",globalInfo.number_entry] );
}
}
return _unzFile!=NULL;
}
-(BOOL) UnzipOpenFile:(NSString*) zipFile Password:(NSString*) password
{
_password = password;
return [self UnzipOpenFile:zipFile];
}
-(BOOL) UnzipFileTo:(NSString*) path overWrite:(BOOL) overwrite
{
BOOL success = YES;
int ret = unzGoToFirstFile( _unzFile );
unsigned char buffer[4096] = {0};
NSFileManager* fman = [NSFileManager defaultManager];
if( ret!=UNZ_OK )
{
[self OutputErrorMessage:@"Failed"];
}
do{
NSAutoreleasePool *internalPool = [[NSAutoreleasePool alloc] init];
if( [_password length]==0 )
ret = unzOpenCurrentFile( _unzFile );
else
ret = unzOpenCurrentFilePassword( _unzFile, [_password cStringUsingEncoding:NSASCIIStringEncoding] );
if( ret!=UNZ_OK )
{
[self OutputErrorMessage:@"Error occurs"];
success = NO;
break;
}
// reading data and write to file
int read ;
unz_file_info fileInfo ={0};
ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
if( ret!=UNZ_OK )
{
[self OutputErrorMessage:@"Error occurs while getting file info"];
success = NO;
unzCloseCurrentFile( _unzFile );
break;
}
char* filename = (char*) malloc( fileInfo.size_filename +1 );
unzGetCurrentFileInfo(_unzFile, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
filename[fileInfo.size_filename] = '\0';
// check if it contains directory
NSString * strPath = [NSString stringWithCString:filename encoding:NSUTF8StringEncoding];
BOOL isDirectory = NO;
if( filename[fileInfo.size_filename-1]=='/' || filename[fileInfo.size_filename-1]=='\\')
isDirectory = YES;
free( filename );
if( [strPath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/\\"]].location!=NSNotFound )
{// contains a path
strPath = [strPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
}
NSString* fullPath = [path stringByAppendingPathComponent:strPath];
if( isDirectory )
[fman createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
else
[fman createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
if( [fman fileExistsAtPath:fullPath] && !isDirectory && !overwrite )
{
if( ![self OverWrite:fullPath] )
{
unzCloseCurrentFile( _unzFile );
ret = unzGoToNextFile( _unzFile );
continue;
}
}
FILE* fp = fopen( (const char*)[fullPath UTF8String], "wb");
while( fp )
{
read=unzReadCurrentFile(_unzFile, buffer, 4096);
if( read > 0 )
{
fwrite(buffer, read, 1, fp );
}
else if( read<0 )
{
[self OutputErrorMessage:@"Failed to reading zip file"];
break;
}
else
break;
}
if( fp )
{
fclose( fp );
// set the orignal datetime property
NSDate* orgDate = nil;
//{{ thanks to brad.eaton for the solution
NSDateComponents *dc = [[NSDateComponents alloc] init];
dc.second = fileInfo.tmu_date.tm_sec;
dc.minute = fileInfo.tmu_date.tm_min;
dc.hour = fileInfo.tmu_date.tm_hour;
dc.day = fileInfo.tmu_date.tm_mday;
dc.month = fileInfo.tmu_date.tm_mon+1;
dc.year = fileInfo.tmu_date.tm_year;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
orgDate = [gregorian dateFromComponents:dc] ;
[dc release];
[gregorian release];
//}}
NSDictionary* attr = [NSDictionary dictionaryWithObject:orgDate forKey:NSFileModificationDate]; //[[NSFileManager defaultManager] fileAttributesAtPath:fullPath traverseLink:YES];
if( attr )
{
// [attr setValue:orgDate forKey:NSFileCreationDate];
if( ![[NSFileManager defaultManager] setAttributes:attr ofItemAtPath:fullPath error:nil] )
{
// cann't set attributes
NSLog(@"Failed to set attributes");
}
}
}
unzCloseCurrentFile( _unzFile );
ret = unzGoToNextFile( _unzFile );
[internalPool drain];
}while( ret==UNZ_OK && UNZ_OK!=UNZ_END_OF_LIST_OF_FILE );
return success;
}
-(BOOL) UnzipCloseFile
{
_password = nil;
if( _unzFile )
return unzClose( _unzFile )==UNZ_OK;
return YES;
}
#pragma mark wrapper for delegate
-(void) OutputErrorMessage:(NSString*) msg
{
if( _delegate && [_delegate respondsToSelector:@selector(ErrorMessage)] )
[_delegate ErrorMessage:msg];
}
-(BOOL) OverWrite:(NSString*) file
{
if( _delegate && [_delegate respondsToSelector:@selector(OverWriteOperation)] )
return [_delegate OverWriteOperation:file];
return YES;
}
#pragma mark get NSDate object for 1980-01-01
-(NSDate*) Date1980
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:1980];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
[comps release];
[gregorian release];
return date;
}
@end