Created
March 16, 2011 10:56
-
-
Save millenomi/872314 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "unzip.h" | |
@interface ILZIPExtractor : NSObject { | |
unzFile zip; | |
} | |
- (id) initWithZIPAtPath:(NSString*) path; | |
@property(readonly) NSArray* files; | |
- (BOOL) extractFileNamed:(NSString*) name toPath:(NSString*) path; | |
- (void) close; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ILZIPExtractor.h" | |
@implementation ILZIPExtractor | |
- (id) initWithZIPAtPath:(NSString*) path; | |
{ | |
if ((self = [super init])) { | |
zip = unzOpen([path fileSystemRepresentation]); | |
if (!zip) { | |
[self release]; | |
return nil; | |
} | |
} | |
return self; | |
} | |
- (void) dealloc | |
{ | |
[self close]; | |
[super dealloc]; | |
} | |
- (NSArray *) files; | |
{ | |
if (unzGoToFirstFile(zip) != UNZ_OK) | |
return nil; | |
NSMutableArray* result = [NSMutableArray array]; | |
do { | |
char fileName[1024]; | |
if (unzGetCurrentFileInfo(zip, NULL, fileName, sizeof(fileName), NULL, 0, NULL, 0) != UNZ_OK) | |
return nil; | |
[result addObject:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:fileName length:strlen(fileName)]]; | |
} while (unzGoToNextFile(zip) == UNZ_OK); | |
return result; | |
} | |
- (BOOL) extractFileNamed:(NSString*) name toPath:(NSString*) path; | |
{ | |
if (unzLocateFile(zip, [name fileSystemRepresentation], 1 /* case sensitive */) != UNZ_OK) | |
return NO; | |
if (unzOpenCurrentFile(zip) != UNZ_OK) | |
return NO; | |
if (![[NSData data] writeToFile:path options:0 error:NULL]) { | |
unzCloseCurrentFile(zip); | |
return NO; | |
} | |
NSFileHandle* fh = [NSFileHandle fileHandleForWritingAtPath:path]; | |
if (!fh) { | |
unzCloseCurrentFile(zip); | |
return NO; | |
} | |
uint8_t bytes[10240]; | |
int result; | |
do { | |
result = unzReadCurrentFile(zip, bytes, sizeof(bytes)); | |
if (result > 0) { | |
NSData* d = [[NSData alloc] initWithBytes:bytes length:result]; | |
[fh writeData:d]; | |
[d release]; | |
} | |
} while (result > 0); | |
[fh closeFile]; | |
if (result < 0) { | |
[[NSFileManager defaultManager] removeItemAtPath:path error:NULL]; | |
return NO; | |
} | |
if (unzCloseCurrentFile(zip) != UNZ_OK) { | |
[[NSFileManager defaultManager] removeItemAtPath:path error:NULL]; | |
return NO; | |
} | |
return YES; | |
} | |
- (void) close; | |
{ | |
if (zip) { | |
unzClose(zip); | |
zip = NULL; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "zip.h" | |
typedef enum { | |
// TODO rename this flag. | |
#define kILZIPFileAppendFilesToExistingArchive kILZIPFileAddFiles | |
kILZIPFileAddFiles = 1 << 0, | |
} ILZIPFileOptions; | |
@interface ILZIPFileCreator : NSObject { | |
zipFile zip; | |
NSString* path; | |
} | |
- (id) initWithZIPFilePath:(NSString*) path options:(ILZIPFileOptions) options; | |
@property(readonly, nonatomic) NSString* path; | |
- (BOOL) addFileNamed:(NSString*) name withContentsOfFileAtPath:(NSString*) file; | |
- (void) close; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ILZIPFileCreator.h" | |
@implementation ILZIPFileCreator | |
- (id) initWithZIPFilePath:(NSString*) p options:(ILZIPFileOptions) o; | |
{ | |
if ((self = [super init])) { | |
zip = zipOpen([p fileSystemRepresentation], (o & kILZIPFileAddFiles)? APPEND_STATUS_ADDINZIP : APPEND_STATUS_CREATE); | |
if (!zip) { | |
[self release]; | |
return nil; | |
} | |
path = [p copy]; | |
} | |
return self; | |
} | |
- (void) dealloc | |
{ | |
[self close]; | |
[super dealloc]; | |
} | |
@synthesize path; | |
- (BOOL) addFileNamed:(NSString*) name withContentsOfFileAtPath:(NSString*) file; | |
{ | |
zip_fileinfo info; | |
info.dosDate = 0; | |
info.external_fa = 0; | |
info.internal_fa = 0; | |
bzero(&(info.tmz_date), sizeof(info.tmz_date)); | |
NSDictionary* attr = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:NULL]; | |
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]; | |
info.tmz_date.tm_sec = [dc second]; | |
info.tmz_date.tm_min = [dc minute]; | |
info.tmz_date.tm_hour = [dc hour]; | |
info.tmz_date.tm_mday = [dc day]; | |
info.tmz_date.tm_mon = [dc month] - 1; | |
info.tmz_date.tm_year = [dc year]; | |
} | |
} else { | |
return NO; | |
} | |
if (zipOpenNewFileInZip(zip, [name fileSystemRepresentation], &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9) != ZIP_OK) | |
return NO; | |
// TODO non-RAM-limited files, non crashy on error. | |
NSData* data = [NSData dataWithContentsOfMappedFile:file]; | |
if (zipWriteInFileInZip(zip, [data bytes], [data length]) != ZIP_OK) | |
goto error; | |
zipCloseFileInZip(zip); | |
return YES; | |
error: | |
zipCloseFileInZip(zip); | |
return NO; | |
} | |
- (void) close; | |
{ | |
if (zip) { | |
zipClose(zip, NULL); | |
zip = NULL; | |
} | |
if (path) { | |
[path release]; | |
path = nil; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment