Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ImageSplitter/ziparchive/ZipArchive.mm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download) (as text)
Fri Feb 15 07:01:49 2013 UTC (11 years, 1 month ago) by zcode
File MIME type: text/x-objcsrc
File size: 8455 byte(s)
[ver 0.2.0] added options
1 //
2 // ZipArchive.mm
3 //
4 //
5 // Created by aish on 08-9-11.
6 // acsolu@gmail.com
7 // Copyright 2008 Inc. All rights reserved.
8 //
9
10 #import "ZipArchive.h"
11 #import "zlib.h"
12 #import "zconf.h"
13
14
15
16 @interface ZipArchive (Private)
17
18 -(void) OutputErrorMessage:(NSString*) msg;
19 -(BOOL) OverWrite:(NSString*) file;
20 -(NSDate*) Date1980;
21 @end
22
23
24
25 @implementation ZipArchive
26 @synthesize delegate = _delegate;
27
28 -(id) init
29 {
30 if( self=[super init] )
31 {
32 _zipFile = NULL ;
33 }
34 return self;
35 }
36
37 -(void) dealloc
38 {
39 [self CloseZipFile2];
40 [super dealloc];
41 }
42
43 -(BOOL) CreateZipFile2:(NSString*) zipFile
44 {
45 _zipFile = zipOpen( (const char*)[zipFile UTF8String], 0 );
46 if( !_zipFile )
47 return NO;
48 return YES;
49 }
50
51 -(BOOL) CreateZipFile2:(NSString*) zipFile Password:(NSString*) password
52 {
53 _password = password;
54 return [self CreateZipFile2:zipFile];
55 }
56
57 -(BOOL) addFileToZip:(NSString*) file newname:(NSString*) newname;
58 {
59 if( !_zipFile )
60 return NO;
61 // tm_zip filetime;
62 time_t current;
63 time( &current );
64
65 zip_fileinfo zipInfo = {0};
66 // zipInfo.dosDate = (unsigned long) current;
67
68 NSDictionary* attr = [[NSFileManager defaultManager] fileAttributesAtPath:file traverseLink:YES];
69 if( attr )
70 {
71 NSDate* fileDate = (NSDate*)[attr objectForKey:NSFileModificationDate];
72 if( fileDate )
73 {
74 // some application does use dosDate, but tmz_date instead
75 // zipInfo.dosDate = [fileDate timeIntervalSinceDate:[self Date1980] ];
76 NSCalendar* currCalendar = [NSCalendar currentCalendar];
77 uint flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
78 NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ;
79 NSDateComponents* dc = [currCalendar components:flags fromDate:fileDate];
80 zipInfo.tmz_date.tm_sec = [dc second];
81 zipInfo.tmz_date.tm_min = [dc minute];
82 zipInfo.tmz_date.tm_hour = [dc hour];
83 zipInfo.tmz_date.tm_mday = [dc day];
84 zipInfo.tmz_date.tm_mon = [dc month] - 1;
85 zipInfo.tmz_date.tm_year = [dc year];
86 }
87 }
88
89 int ret ;
90 NSData* data = nil;
91 if( [_password length] == 0 )
92 {
93 ret = zipOpenNewFileInZip( _zipFile,
94 (const char*) [newname UTF8String],
95 &zipInfo,
96 NULL,0,
97 NULL,0,
98 NULL,//comment
99 Z_DEFLATED,
100 Z_DEFAULT_COMPRESSION );
101 }
102 else
103 {
104 data = [ NSData dataWithContentsOfFile:file];
105 uLong crcValue = crc32( 0L,NULL, 0L );
106 crcValue = crc32( crcValue, (const Bytef*)[data bytes], [data length] );
107 ret = zipOpenNewFileInZip3( _zipFile,
108 (const char*) [newname UTF8String],
109 &zipInfo,
110 NULL,0,
111 NULL,0,
112 NULL,//comment
113 Z_DEFLATED,
114 Z_DEFAULT_COMPRESSION,
115 0,
116 15,
117 8,
118 Z_DEFAULT_STRATEGY,
119 [_password cStringUsingEncoding:NSASCIIStringEncoding],
120 crcValue );
121 }
122 if( ret!=Z_OK )
123 {
124 return NO;
125 }
126 if( data==nil )
127 {
128 data = [ NSData dataWithContentsOfFile:file];
129 }
130 unsigned int dataLen = [data length];
131 ret = zipWriteInFileInZip( _zipFile, (const void*)[data bytes], dataLen);
132 if( ret!=Z_OK )
133 {
134 return NO;
135 }
136 ret = zipCloseFileInZip( _zipFile );
137 if( ret!=Z_OK )
138 return NO;
139 return YES;
140 }
141
142 -(BOOL) CloseZipFile2
143 {
144 _password = nil;
145 if( _zipFile==NULL )
146 return NO;
147 BOOL ret = zipClose( _zipFile,NULL )==Z_OK?YES:NO;
148 _zipFile = NULL;
149 return ret;
150 }
151
152 -(BOOL) UnzipOpenFile:(NSString*) zipFile
153 {
154 _unzFile = unzOpen( (const char*)[zipFile UTF8String] );
155 if( _unzFile )
156 {
157 unz_global_info globalInfo = {0};
158 if( unzGetGlobalInfo(_unzFile, &globalInfo )==UNZ_OK )
159 {
160 NSLog([NSString stringWithFormat:@"%d entries in the zip file",globalInfo.number_entry] );
161 }
162 }
163 return _unzFile!=NULL;
164 }
165
166 -(BOOL) UnzipOpenFile:(NSString*) zipFile Password:(NSString*) password
167 {
168 _password = password;
169 return [self UnzipOpenFile:zipFile];
170 }
171
172 -(BOOL) UnzipFileTo:(NSString*) path overWrite:(BOOL) overwrite
173 {
174 BOOL success = YES;
175 int ret = unzGoToFirstFile( _unzFile );
176 unsigned char buffer[4096] = {0};
177 NSFileManager* fman = [NSFileManager defaultManager];
178 if( ret!=UNZ_OK )
179 {
180 [self OutputErrorMessage:@"Failed"];
181 }
182
183 do{
184 if( [_password length]==0 )
185 ret = unzOpenCurrentFile( _unzFile );
186 else
187 ret = unzOpenCurrentFilePassword( _unzFile, [_password cStringUsingEncoding:NSASCIIStringEncoding] );
188 if( ret!=UNZ_OK )
189 {
190 [self OutputErrorMessage:@"Error occurs"];
191 success = NO;
192 break;
193 }
194 // reading data and write to file
195 int read ;
196 unz_file_info fileInfo ={0};
197 ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
198 if( ret!=UNZ_OK )
199 {
200 [self OutputErrorMessage:@"Error occurs while getting file info"];
201 success = NO;
202 unzCloseCurrentFile( _unzFile );
203 break;
204 }
205 char* filename = (char*) malloc( fileInfo.size_filename +1 );
206 unzGetCurrentFileInfo(_unzFile, &fileInfo, filename, fileInfo.size_filename + 1, NULL, 0, NULL, 0);
207 filename[fileInfo.size_filename] = '\0';
208
209 // check if it contains directory
210 NSString * strPath = [NSString stringWithCString:filename];
211 BOOL isDirectory = NO;
212 if( filename[fileInfo.size_filename-1]=='/' || filename[fileInfo.size_filename-1]=='\\')
213 isDirectory = YES;
214 free( filename );
215 if( [strPath rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"/\\"]].location!=NSNotFound )
216 {// contains a path
217 strPath = [strPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
218 }
219 NSString* fullPath = [path stringByAppendingPathComponent:strPath];
220
221 if( isDirectory )
222 [fman createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil];
223 else
224 [fman createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
225 if( [fman fileExistsAtPath:fullPath] && !isDirectory && !overwrite )
226 {
227 if( ![self OverWrite:fullPath] )
228 {
229 unzCloseCurrentFile( _unzFile );
230 ret = unzGoToNextFile( _unzFile );
231 continue;
232 }
233 }
234 FILE* fp = fopen( (const char*)[fullPath UTF8String], "wb");
235 while( fp )
236 {
237 read=unzReadCurrentFile(_unzFile, buffer, 4096);
238 if( read > 0 )
239 {
240 fwrite(buffer, read, 1, fp );
241 }
242 else if( read<0 )
243 {
244 [self OutputErrorMessage:@"Failed to reading zip file"];
245 break;
246 }
247 else
248 break;
249 }
250 if( fp )
251 {
252 fclose( fp );
253 // set the orignal datetime property
254 NSDate* orgDate = nil;
255
256 //{{ thanks to brad.eaton for the solution
257 NSDateComponents *dc = [[NSDateComponents alloc] init];
258
259 dc.second = fileInfo.tmu_date.tm_sec;
260 dc.minute = fileInfo.tmu_date.tm_min;
261 dc.hour = fileInfo.tmu_date.tm_hour;
262 dc.day = fileInfo.tmu_date.tm_mday;
263 dc.month = fileInfo.tmu_date.tm_mon+1;
264 dc.year = fileInfo.tmu_date.tm_year;
265
266 NSCalendar *gregorian = [[NSCalendar alloc]
267 initWithCalendarIdentifier:NSGregorianCalendar];
268
269 orgDate = [gregorian dateFromComponents:dc] ;
270 [dc release];
271 [gregorian release];
272 //}}
273
274
275 NSDictionary* attr = [NSDictionary dictionaryWithObject:orgDate forKey:NSFileModificationDate]; //[[NSFileManager defaultManager] fileAttributesAtPath:fullPath traverseLink:YES];
276 if( attr )
277 {
278 // [attr setValue:orgDate forKey:NSFileCreationDate];
279 if( ![[NSFileManager defaultManager] setAttributes:attr ofItemAtPath:fullPath error:nil] )
280 {
281 // cann't set attributes
282 NSLog(@"Failed to set attributes");
283 }
284
285 }
286
287
288
289 }
290 unzCloseCurrentFile( _unzFile );
291 ret = unzGoToNextFile( _unzFile );
292 }while( ret==UNZ_OK && UNZ_OK!=UNZ_END_OF_LIST_OF_FILE );
293 return success;
294 }
295
296 -(BOOL) UnzipCloseFile
297 {
298 _password = nil;
299 if( _unzFile )
300 return unzClose( _unzFile )==UNZ_OK;
301 return YES;
302 }
303
304 #pragma mark wrapper for delegate
305 -(void) OutputErrorMessage:(NSString*) msg
306 {
307 if( _delegate && [_delegate respondsToSelector:@selector(ErrorMessage)] )
308 [_delegate ErrorMessage:msg];
309 }
310
311 -(BOOL) OverWrite:(NSString*) file
312 {
313 if( _delegate && [_delegate respondsToSelector:@selector(OverWriteOperation)] )
314 return [_delegate OverWriteOperation:file];
315 return YES;
316 }
317
318 #pragma mark get NSDate object for 1980-01-01
319 -(NSDate*) Date1980
320 {
321 NSDateComponents *comps = [[NSDateComponents alloc] init];
322 [comps setDay:1];
323 [comps setMonth:1];
324 [comps setYear:1980];
325 NSCalendar *gregorian = [[NSCalendar alloc]
326 initWithCalendarIdentifier:NSGregorianCalendar];
327 NSDate *date = [gregorian dateFromComponents:comps];
328
329 [comps release];
330 [gregorian release];
331 return date;
332 }
333
334
335 @end
336
337

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26