From faa7e4d89758e0424967b394e7d07c9f01a10419 Mon Sep 17 00:00:00 2001 From: Gianluca Bertani Date: Fri, 28 Aug 2015 19:43:47 +0200 Subject: [PATCH] Introduced method variants with NSError handling; simplified exception creation --- Classes/Objective_ZipViewController.h | 1 + Classes/Objective_ZipViewController.m | 89 +++++- Objective-Zip/OZFileInZipInfo.h | 1 + Objective-Zip/OZZipException+Internals.h | 22 ++ Objective-Zip/OZZipException.h | 2 + Objective-Zip/OZZipException.m | 22 ++ Objective-Zip/OZZipFile.h | 42 ++- Objective-Zip/OZZipFile.m | 361 ++++++++++++++--------- Objective-Zip/OZZipReadStream.h | 7 + Objective-Zip/OZZipReadStream.m | 32 +- Objective-Zip/OZZipWriteStream.h | 7 + Objective-Zip/OZZipWriteStream.m | 32 +- Objective_ZipViewController.xib | 59 +++- 13 files changed, 495 insertions(+), 182 deletions(-) diff --git a/Classes/Objective_ZipViewController.h b/Classes/Objective_ZipViewController.h index a26c4e4..1e3d415 100644 --- a/Classes/Objective_ZipViewController.h +++ b/Classes/Objective_ZipViewController.h @@ -44,6 +44,7 @@ - (IBAction) zipUnzip2; - (IBAction) zipCheck1; - (IBAction) zipCheck2; +- (IBAction) errorWrapping; @end diff --git a/Classes/Objective_ZipViewController.m b/Classes/Objective_ZipViewController.m index ddee2d1..0ee39cb 100644 --- a/Classes/Objective_ZipViewController.m +++ b/Classes/Objective_ZipViewController.m @@ -48,6 +48,7 @@ - (void) test2; - (void) test3; - (void) test4; +- (void) test5; #pragma mark - @@ -97,6 +98,12 @@ [_testThread start]; } +- (IBAction) errorWrapping { + + _testThread= [[NSThread alloc] initWithTarget:self selector:@selector(test5) object:nil]; + [_testThread start]; +} + #pragma mark - #pragma mark Test 1: zip & unzip @@ -156,14 +163,14 @@ [self log:@"Test 1: - %@ %@ %d (%d)", info.name, info.date, info.size, info.level]; [self log:@"Test 1: opening first file..."]; - - [unzipFile goToFirstFileInZip]; + + [unzipFile goToFirstFileInZip]; OZZipReadStream *read1= [unzipFile readCurrentFileInZip]; [self log:@"Test 1: reading from first file's stream..."]; NSMutableData *data1= [[NSMutableData alloc] initWithLength:256]; - int bytesRead1= [read1 readDataWithBuffer:data1]; + NSUInteger bytesRead1= [read1 readDataWithBuffer:data1]; BOOL ok= NO; if (bytesRead1 == 3) { @@ -189,7 +196,7 @@ [self log:@"Test 1: reading from second file's stream..."]; NSMutableData *data2= [[NSMutableData alloc] initWithLength:256]; - int bytesRead2= [read2 readDataWithBuffer:data2]; + NSUInteger bytesRead2= [read2 readDataWithBuffer:data2]; ok= NO; if (bytesRead2 == 3) { @@ -216,7 +223,7 @@ } @catch (OZZipException *ze) { [self log:@"Test 1: caught a ZipException (see logs), terminating..."]; - NSLog(@"Test 1: ZipException caught: %d - %@", ze.error, [ze reason]); + NSLog(@"Test 1: ZipException caught: %ld - %@", (long) ze.error, [ze reason]); } @catch (id e) { [self log:@"Test 1: caught a generic exception (see logs), terminating..."]; @@ -286,7 +293,7 @@ [self log:@"Test 2: reading from file's stream..."]; for (int i= 0; i < HUGE_TEST_NUMBER_OF_BLOCKS; i++) { - int bytesRead= [read readDataWithBuffer:buffer]; + NSUInteger bytesRead= [read readDataWithBuffer:buffer]; BOOL ok= NO; if (bytesRead == [data length]) { @@ -315,7 +322,7 @@ } @catch (OZZipException *ze) { [self log:@"Test 2: caught a ZipException (see logs), terminating..."]; - NSLog(@"Test 2: ZipException caught: %d - %@", ze.error, [ze reason]); + NSLog(@"Test 2: ZipException caught: %ld - %@", (long) ze.error, [ze reason]); } @catch (id e) { [self log:@"Test 2: caught a generic exception (see logs), terminating..."]; @@ -352,7 +359,7 @@ NSMutableData *buffer= [[NSMutableData alloc] initWithLength:1024]; - int bytesRead= [read readDataWithBuffer:buffer]; + NSUInteger bytesRead= [read readDataWithBuffer:buffer]; NSString *fileText= [[NSString alloc] initWithBytes:[buffer bytes] length:bytesRead encoding:NSUTF8StringEncoding]; if ([fileText isEqualToString:@"Objective-Zip Mac test file\n"]) @@ -373,7 +380,7 @@ } @catch (OZZipException *ze) { [self log:@"Test 3: caught a ZipException (see logs), terminating..."]; - NSLog(@"Test 3: ZipException caught: %d - %@", ze.error, [ze reason]); + NSLog(@"Test 3: ZipException caught: %ld - %@", (long) ze.error, [ze reason]); } @catch (id e) { [self log:@"Test 3: caught a generic exception (see logs), terminating..."]; @@ -406,7 +413,7 @@ NSMutableData *buffer= [[NSMutableData alloc] initWithLength:1024]; - int bytesRead= [read readDataWithBuffer:buffer]; + NSUInteger bytesRead= [read readDataWithBuffer:buffer]; NSString *fileText= [[NSString alloc] initWithBytes:[buffer bytes] length:bytesRead encoding:NSUTF8StringEncoding]; if ([fileText isEqualToString:@"Objective-Zip Windows test file\r\n"]) @@ -427,7 +434,7 @@ } @catch (OZZipException *ze) { [self log:@"Test 4: caught a ZipException (see logs), terminating..."]; - NSLog(@"Test 4: ZipException caught: %d - %@", ze.error, [ze reason]); + NSLog(@"Test 4: ZipException caught: %ld - %@", (long) ze.error, [ze reason]); } @catch (id e) { [self log:@"Test 4: caught a generic exception (see logs), terminating..."]; @@ -437,6 +444,66 @@ } } +#pragma mark - +#pragma mark Test 5: error wrapping + +- (void) test5 { + @autoreleasepool { + + NSString *filePath= @"/root.zip"; + + @try { + [self log:@"Test 5: opening impossible zip file for writing..."]; + + OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:filePath mode:OZZipFileModeCreate]; + + [self log:@"Test 5: test failed, no error reported"]; + + [zipFile close]; + + } @catch (OZZipException *ze) { + if (ze.error != ERROR_NO_SUCH_FILE) { + [self log:@"Test 5: test failed, wrong error reported"]; + + } else { + [self log:@"Test 5: correct error reported"]; + } + + } @catch (id e) { + [self log:@"Test 5: caught a generic exception (see logs)"]; + + NSLog(@"Test 5: Exception caught: %@ - %@", [[e class] description], [e description]); + } + + @try { + [self log:@"Test 5: opening again impossible zip file for writing..."]; + + NSError *error= nil; + OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:filePath mode:OZZipFileModeCreate error:&error]; + + if (zipFile || (!error)) { + [self log:@"Test 5: test failed, no error reported"]; + + [zipFile close]; + + } else { + if (error.code != ERROR_NO_SUCH_FILE) { + [self log:@"Test 5: test failed, wrong error reported"]; + + } else { + [self log:@"Test 5: correct error reported"]; + [self log:@"Test 5: test terminated succesfully"]; + } + } + + } @catch (id e) { + [self log:@"Test 5: caught a generic exception (see logs), terminating..."]; + + NSLog(@"Test 5: Exception caught: %@ - %@", [[e class] description], [e description]); + } + } +} + #pragma mark - #pragma mark Logging diff --git a/Objective-Zip/OZFileInZipInfo.h b/Objective-Zip/OZFileInZipInfo.h index 86f4c42..42149d3 100644 --- a/Objective-Zip/OZFileInZipInfo.h +++ b/Objective-Zip/OZFileInZipInfo.h @@ -38,6 +38,7 @@ @interface OZFileInZipInfo : NSObject + #pragma mark - #pragma mark Properties diff --git a/Objective-Zip/OZZipException+Internals.h b/Objective-Zip/OZZipException+Internals.h index ad55c0f..4cc4400 100644 --- a/Objective-Zip/OZZipException+Internals.h +++ b/Objective-Zip/OZZipException+Internals.h @@ -33,12 +33,34 @@ #import "OZZipException.h" +#define ERROR_WRAP_BEGIN \ + @try { + +#define ERROR_WRAP_END(err) \ + } @catch (OZZipException *ze) { \ + if (ze.error) { \ + if (err) { \ + *err= [NSError errorWithDomain:@"ObjectiveZipErrorDomain" \ + code:ze.error \ + userInfo:@{NSLocalizedDescriptionKey: ze.name, \ + NSLocalizedFailureReasonErrorKey: ze.reason}]; \ + } \ + } else \ + @throw ze; \ + } @catch (NSException *exc) { \ + @throw exc; \ + } + + @interface OZZipException (Internals) #pragma mark - #pragma mark Initialization ++ (OZZipException *) zipExceptionWithReason:(NSString *)format, ...; ++ (OZZipException *) zipExceptionWithError:(NSInteger)error reason:(NSString *)format, ...; + - (instancetype) initWithReason:(NSString *)reason; - (instancetype) initWithError:(NSInteger)error reason:(NSString *)reason; diff --git a/Objective-Zip/OZZipException.h b/Objective-Zip/OZZipException.h index 207d7c5..e1afe78 100644 --- a/Objective-Zip/OZZipException.h +++ b/Objective-Zip/OZZipException.h @@ -33,6 +33,8 @@ #import +#define ERROR_NO_SUCH_FILE (-9001) + @interface OZZipException : NSException diff --git a/Objective-Zip/OZZipException.m b/Objective-Zip/OZZipException.m index 335ffae..da82ad6 100644 --- a/Objective-Zip/OZZipException.m +++ b/Objective-Zip/OZZipException.m @@ -56,6 +56,28 @@ #pragma mark - #pragma mark Initialization ++ (OZZipException *) zipExceptionWithReason:(NSString *)format, ... { + + // Variable arguments formatting + va_list arguments; + va_start(arguments, format); + NSString *reason= [[NSString alloc] initWithFormat:format arguments:arguments]; + va_end(arguments); + + return [[OZZipException alloc] initWithReason:reason]; +} + ++ (OZZipException *) zipExceptionWithError:(NSInteger)error reason:(NSString *)format, ... { + + // Variable arguments formatting + va_list arguments; + va_start(arguments, format); + NSString *reason= [[NSString alloc] initWithFormat:format arguments:arguments]; + va_end(arguments); + + return [[OZZipException alloc] initWithError:error reason:reason]; +} + - (instancetype) initWithReason:(NSString *)reason { if (self= [super initWithName:@"OZZipException" reason:reason userInfo:nil]) { _error= 0; diff --git a/Objective-Zip/OZZipFile.h b/Objective-Zip/OZZipFile.h index 7fa39da..91e9f6f 100644 --- a/Objective-Zip/OZZipFile.h +++ b/Objective-Zip/OZZipFile.h @@ -50,6 +50,12 @@ - (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode; - (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode legacy32BitMode:(BOOL)legacy32BitMode; +#pragma mark - +#pragma mark Initialization (NSError variants) + +- (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode error:(NSError * __autoreleasing *)error; +- (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode legacy32BitMode:(BOOL)legacy32BitMode error:(NSError * __autoreleasing *)error; + #pragma mark - #pragma mark File writing @@ -59,6 +65,14 @@ - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel password:(NSString *)password crc32:(NSUInteger)crc32; +#pragma mark - +#pragma mark File writing (NSError variants) + +- (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip compressionLevel:(OZZipCompressionLevel)compressionLevel error:(NSError * __autoreleasing *)error; +- (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel error:(NSError * __autoreleasing *)error; +- (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel password:(NSString *)password crc32:(NSUInteger)crc32 error:(NSError * __autoreleasing *)error; + + #pragma mark - #pragma mark File seeking and info @@ -66,10 +80,23 @@ - (BOOL) goToNextFileInZip; - (BOOL) locateFileInZip:(NSString *)fileNameInZip; +- (NSUInteger) numFilesInZip; - (NSArray *) listFileInZipInfos; - (OZFileInZipInfo *) getCurrentFileInZipInfo; +#pragma mark - +#pragma mark File seeking and info (NSError variants) + +- (void) goToFirstFileInZipWithError:(NSError * __autoreleasing *)error; +- (BOOL) goToNextFileInZipWithError:(NSError * __autoreleasing *)error; +- (BOOL) locateFileInZip:(NSString *)fileNameInZip error:(NSError * __autoreleasing *)error; + +- (NSUInteger) numFilesInZipWithError:(NSError * __autoreleasing *)error; +- (NSArray *) listFileInZipInfosWithError:(NSError * __autoreleasing *)error; +- (OZFileInZipInfo *) getCurrentFileInZipInfoWithError:(NSError * __autoreleasing *)error; + + #pragma mark - #pragma mark File reading @@ -77,12 +104,25 @@ - (OZZipReadStream *) readCurrentFileInZipWithPassword:(NSString *)password; +#pragma mark - +#pragma mark File reading (NSError variants) + +- (OZZipReadStream *) readCurrentFileInZipWithError:(NSError * __autoreleasing *)error; +- (OZZipReadStream *) readCurrentFileInZipWithPassword:(NSString *)password error:(NSError * __autoreleasing *)error; + + #pragma mark - #pragma mark Closing - (void) close; +#pragma mark - +#pragma mark Closing (NSError variants) + +- (void) closeWithError:(NSError * __autoreleasing *)error; + + #pragma mark - #pragma mark Properties @@ -90,7 +130,5 @@ @property (nonatomic, readonly) OZZipFileMode mode; @property (nonatomic, readonly) BOOL legacy32BitMode; -@property (nonatomic, readonly) NSUInteger numFilesInZip; - @end diff --git a/Objective-Zip/OZZipFile.m b/Objective-Zip/OZZipFile.m index 443a6e1..d0b1fad 100644 --- a/Objective-Zip/OZZipFile.m +++ b/Objective-Zip/OZZipFile.m @@ -90,36 +90,28 @@ // Support for legacy 32 bit mode: here we use 32 or 64 bit version // alternatively, as internal (common) version is not exposed _unzFile= (_legacy32BitMode ? unzOpen(path) : unzOpen64(path)); - if (_unzFile == NULL) { - NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_unzFile == NULL) + @throw [OZZipException zipExceptionWithError:ERROR_NO_SUCH_FILE reason:@"Can't open '%@'", _fileName]; break; case OZZipFileModeCreate: // Support for legacy 32 bit mode: here we use the common version _zipFile= zipOpen3(path, APPEND_STATUS_CREATE, 0, NULL, NULL); - if (_zipFile == NULL) { - NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_zipFile == NULL) + @throw [OZZipException zipExceptionWithError:ERROR_NO_SUCH_FILE reason:@"Can't open '%@'", _fileName]; break; case OZZipFileModeAppend: // Support for legacy 32 bit mode: here we use the common version _zipFile= zipOpen3(path, APPEND_STATUS_ADDINZIP, 0, NULL, NULL); - if (_zipFile == NULL) { - NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_zipFile == NULL) + @throw [OZZipException zipExceptionWithError:ERROR_NO_SUCH_FILE reason:@"Can't open '%@'", _fileName]; break; - default: { - NSString *reason= [NSString stringWithFormat:@"Unknown mode %d", _mode]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + default: + @throw [OZZipException zipExceptionWithReason:@"Unknown mode %d", _mode]; } } @@ -127,14 +119,32 @@ } +#pragma mark - +#pragma mark Initialization (NSError variants) + +- (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode error:(NSError *__autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self initWithFileName:fileName mode:mode]; + + } ERROR_WRAP_END(error); +} + +- (instancetype) initWithFileName:(NSString *)fileName mode:(OZZipFileMode)mode legacy32BitMode:(BOOL)legacy32BitMode error:(NSError *__autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self initWithFileName:fileName mode:mode legacy32BitMode:legacy32BitMode]; + + } ERROR_WRAP_END(error); +} + + #pragma mark - #pragma mark File writing - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip compressionLevel:(OZZipCompressionLevel)compressionLevel { - if (_mode == OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted with Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode == OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation not permitted in Unzip mode"]; NSDate *now= [NSDate date]; NSCalendar *calendar= [NSCalendar currentCalendar]; @@ -162,19 +172,15 @@ NULL, 0, (_legacy32BitMode ? 0 : 1)); - if (err != ZIP_OK) { - NSString *reason= [NSString stringWithFormat:@"Error opening '%@' in zipfile", fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != ZIP_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error opening '%@' in zipfile", fileNameInZip]; return [[OZZipWriteStream alloc] initWithZipFileStruct:_zipFile fileNameInZip:fileNameInZip]; } - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel { - if (_mode == OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted with Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode == OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation not permitted in Unzip mode"]; NSCalendar *calendar= [NSCalendar currentCalendar]; NSDateComponents *date= [calendar components:(NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:fileDate]; @@ -201,19 +207,15 @@ NULL, 0, (_legacy32BitMode ? 0 : 1)); - if (err != ZIP_OK) { - NSString *reason= [NSString stringWithFormat:@"Error opening '%@' in zipfile", fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != ZIP_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error opening '%@' in zipfile", fileNameInZip]; return [[OZZipWriteStream alloc] initWithZipFileStruct:_zipFile fileNameInZip:fileNameInZip]; } - (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel password:(NSString *)password crc32:(NSUInteger)crc32 { - if (_mode == OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted with Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode == OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation not permitted in Unzip mode"]; NSCalendar *calendar= [NSCalendar currentCalendar]; NSDateComponents *date= [calendar components:(NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:fileDate]; @@ -240,71 +242,111 @@ [password cStringUsingEncoding:NSUTF8StringEncoding], crc32, (_legacy32BitMode ? 0 : 1)); - if (err != ZIP_OK) { - NSString *reason= [NSString stringWithFormat:@"Error opening '%@' in zipfile", fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != ZIP_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error opening '%@' in zipfile", fileNameInZip]; return [[OZZipWriteStream alloc] initWithZipFileStruct:_zipFile fileNameInZip:fileNameInZip]; } +#pragma mark - +#pragma mark File writing (NSError variants) + +- (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip compressionLevel:(OZZipCompressionLevel)compressionLevel error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self writeFileInZipWithName:fileNameInZip compressionLevel:compressionLevel]; + + } ERROR_WRAP_END(error); +} + +- (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self writeFileInZipWithName:fileNameInZip fileDate:fileDate compressionLevel:compressionLevel]; + + } ERROR_WRAP_END(error); +} + +- (OZZipWriteStream *) writeFileInZipWithName:(NSString *)fileNameInZip fileDate:(NSDate *)fileDate compressionLevel:(OZZipCompressionLevel)compressionLevel password:(NSString *)password crc32:(NSUInteger)crc32 error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self writeFileInZipWithName:fileNameInZip fileDate:fileDate compressionLevel:compressionLevel password:password crc32:crc32]; + + } ERROR_WRAP_END(error); +} + + #pragma mark - #pragma mark File seeking and info - (void) goToFirstFileInZip { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; int err= unzGoToFirstFile(_unzFile); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error going to first file in zip in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error going to first file in zip of '%@'", _fileName]; } - (BOOL) goToNextFileInZip { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; int err= unzGoToNextFile(_unzFile); if (err == UNZ_END_OF_LIST_OF_FILE) return NO; - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error going to next file in zip in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error going to next file in zip of '%@'", _fileName]; return YES; } - (BOOL) locateFileInZip:(NSString *)fileNameInZip { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; int err= unzLocateFile(_unzFile, [fileNameInZip cStringUsingEncoding:NSUTF8StringEncoding], NULL); if (err == UNZ_END_OF_LIST_OF_FILE) return NO; - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error localting file in zip in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error localting file in zip of '%@'", _fileName]; return YES; } +- (NSUInteger) numFilesInZip { + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; + + // Support for legacy 32 bit mode: here we use the 32 or 64 bit + // version alternatively, as there is not internal (common) version + if (_legacy32BitMode) { + unz_global_info gi; + + int err= unzGetGlobalInfo(_unzFile, &gi); + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error getting global info of '%@'", _fileName]; + + return gi.number_entry; + + } else { + unz_global_info64 gi; + + int err= unzGetGlobalInfo64(_unzFile, &gi); + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error getting global info of '%@'", _fileName]; + + return gi.number_entry; + } +} + - (NSArray *) listFileInZipInfos { NSUInteger num= [self numFilesInZip]; if (num < 1) - return [[NSArray alloc] init]; + return [NSArray array]; NSMutableArray *files= [[NSMutableArray alloc] initWithCapacity:num]; @@ -321,10 +363,8 @@ } - (OZFileInZipInfo *) getCurrentFileInZipInfo { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; char filename_inzip[FILE_IN_ZIP_MAX_NAME_LENGTH]; unz_file_info64 file_info; @@ -332,10 +372,8 @@ // Support for legacy 32 bit mode: here we use the 64 bit version, // as it also internally called from the 32 bit version int err= unzGetCurrentFileInfo64(_unzFile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error getting current file info in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error getting current file info of '%@'", _fileName]; NSString *name= [NSString stringWithCString:filename_inzip encoding:NSUTF8StringEncoding]; @@ -373,14 +411,64 @@ } +#pragma mark - +#pragma mark File seeking and info (NSError variants) + +- (void) goToFirstFileInZipWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + [self goToFirstFileInZip]; + + } ERROR_WRAP_END(error); +} + +- (BOOL) goToNextFileInZipWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self goToNextFileInZip]; + + } ERROR_WRAP_END(error); +} + +- (BOOL) locateFileInZip:(NSString *)fileNameInZip error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self locateFileInZip:fileNameInZip]; + + } ERROR_WRAP_END(error); +} + +- (NSUInteger) numFilesInZipWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self numFilesInZip]; + + } ERROR_WRAP_END(error); +} + +- (NSArray *) listFileInZipInfosWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self listFileInZipInfos]; + + } ERROR_WRAP_END(error); +} + +- (OZFileInZipInfo *) getCurrentFileInZipInfoWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self getCurrentFileInZipInfo]; + + } ERROR_WRAP_END(error); +} + + #pragma mark - #pragma mark File reading - (OZZipReadStream *) readCurrentFileInZip { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; char filename_inzip[FILE_IN_ZIP_MAX_NAME_LENGTH]; unz_file_info64 file_info; @@ -388,27 +476,21 @@ // Support for legacy 32 bit mode: here we use the 64 bit version, // as it also internally called from the 32 bit version int err= unzGetCurrentFileInfo64(_unzFile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error getting current file info in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error getting current file info of '%@'", _fileName]; NSString *fileNameInZip= [NSString stringWithCString:filename_inzip encoding:NSUTF8StringEncoding]; err= unzOpenCurrentFilePassword(_unzFile, NULL); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error opening current file in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error opening current file of '%@'", _fileName]; return [[OZZipReadStream alloc] initWithUnzFileStruct:_unzFile fileNameInZip:fileNameInZip]; } - (OZZipReadStream *) readCurrentFileInZipWithPassword:(NSString *)password { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + if (_mode != OZZipFileModeUnzip) + @throw [OZZipException zipExceptionWithReason:@"Operation permitted only in Unzip mode"]; char filename_inzip[FILE_IN_ZIP_MAX_NAME_LENGTH]; unz_file_info64 file_info; @@ -416,23 +498,39 @@ // Support for legacy 32 bit mode: here we use the 64 bit version, // as it also internally called from the 32 bit version int err= unzGetCurrentFileInfo64(_unzFile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error getting current file info in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error getting current file info of '%@'", _fileName]; NSString *fileNameInZip= [NSString stringWithCString:filename_inzip encoding:NSUTF8StringEncoding]; err= unzOpenCurrentFilePassword(_unzFile, [password cStringUsingEncoding:NSUTF8StringEncoding]); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error opening current file in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error opening current file of '%@'", _fileName]; return [[OZZipReadStream alloc] initWithUnzFileStruct:_unzFile fileNameInZip:fileNameInZip]; } +#pragma mark - +#pragma mark File reading (NSError variants) + +- (OZZipReadStream *) readCurrentFileInZipWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self readCurrentFileInZip]; + + } ERROR_WRAP_END(error); +} + +- (OZZipReadStream *) readCurrentFileInZipWithPassword:(NSString *)password error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + return [self readCurrentFileInZipWithPassword:password]; + + } ERROR_WRAP_END(error); +} + + #pragma mark - #pragma mark Closing @@ -440,38 +538,41 @@ switch (_mode) { case OZZipFileModeUnzip: { int err= unzClose(_unzFile); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error closing '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error closing '%@'", _fileName]; break; } case OZZipFileModeCreate: { int err= zipClose(_zipFile, NULL); - if (err != ZIP_OK) { - NSString *reason= [NSString stringWithFormat:@"Error closing '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != ZIP_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error closing '%@'", _fileName]; break; } case OZZipFileModeAppend: { int err= zipClose(_zipFile, NULL); - if (err != ZIP_OK) { - NSString *reason= [NSString stringWithFormat:@"Error closing '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != ZIP_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error closing '%@'", _fileName]; break; } - default: { - NSString *reason= [NSString stringWithFormat:@"Unknown mode %d", _mode]; - @throw [[OZZipException alloc] initWithReason:reason]; - } + default: + @throw [OZZipException zipExceptionWithReason:@"Unknown mode %d", _mode]; } } +#pragma mark - +#pragma mark Closing (NSError variants) + +- (void) closeWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + [self close]; + + } ERROR_WRAP_END(error); +} + #pragma mark - #pragma mark Properties @@ -480,39 +581,5 @@ @synthesize mode= _mode; @synthesize legacy32BitMode= _legacy32BitMode; -@dynamic numFilesInZip; - -- (NSUInteger) numFilesInZip { - if (_mode != OZZipFileModeUnzip) { - NSString *reason= [NSString stringWithFormat:@"Operation not permitted without Unzip mode"]; - @throw [[OZZipException alloc] initWithReason:reason]; - } - - // Support for legacy 32 bit mode: here we use the 32 or 64 bit - // version alternatively, as there is not internal (common) version - if (_legacy32BitMode) { - unz_global_info gi; - - int err= unzGetGlobalInfo(_unzFile, &gi); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error getting global info in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } - - return gi.number_entry; - - } else { - unz_global_info64 gi; - - int err= unzGetGlobalInfo64(_unzFile, &gi); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error getting global info in '%@'", _fileName]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } - - return gi.number_entry; - } -} - @end diff --git a/Objective-Zip/OZZipReadStream.h b/Objective-Zip/OZZipReadStream.h index 831f1ee..1bdf295 100644 --- a/Objective-Zip/OZZipReadStream.h +++ b/Objective-Zip/OZZipReadStream.h @@ -44,4 +44,11 @@ - (void) finishedReading; +#pragma mark - +#pragma mark Reading data (NSError variants) + +- (NSUInteger) readDataWithBuffer:(NSMutableData *)buffer error:(NSError * __autoreleasing *)error; +- (void) finishedReadingWithError:(NSError * __autoreleasing *)error; + + @end diff --git a/Objective-Zip/OZZipReadStream.m b/Objective-Zip/OZZipReadStream.m index 3b124cb..b0cc63b 100644 --- a/Objective-Zip/OZZipReadStream.m +++ b/Objective-Zip/OZZipReadStream.m @@ -75,20 +75,36 @@ - (NSUInteger) readDataWithBuffer:(NSMutableData *)buffer { int err= unzReadCurrentFile(_unzFile, [buffer mutableBytes], (uInt) [buffer length]); - if (err < 0) { - NSString *reason= [NSString stringWithFormat:@"Error reading '%@' in the zipfile", _fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err < 0) + @throw [OZZipException zipExceptionWithError:err reason:@"Error reading '%@' in the zipfile", _fileNameInZip]; return err; } - (void) finishedReading { int err= unzCloseCurrentFile(_unzFile); - if (err != UNZ_OK) { - NSString *reason= [NSString stringWithFormat:@"Error closing '%@' in the zipfile", _fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != UNZ_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error closing '%@' in the zipfile", _fileNameInZip]; +} + + +#pragma mark - +#pragma mark Reading data (NSError variants) + +- (NSUInteger) readDataWithBuffer:(NSMutableData *)buffer error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + [self readDataWithBuffer:buffer]; + + } ERROR_WRAP_END(error); +} + +- (void) finishedReadingWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + [self finishedReading]; + + } ERROR_WRAP_END(error); } diff --git a/Objective-Zip/OZZipWriteStream.h b/Objective-Zip/OZZipWriteStream.h index cd293e5..d9dc0b8 100644 --- a/Objective-Zip/OZZipWriteStream.h +++ b/Objective-Zip/OZZipWriteStream.h @@ -44,4 +44,11 @@ - (void) finishedWriting; +#pragma mark - +#pragma mark Writing data (NSError variants) + +- (void) writeData:(NSData *)data error:(NSError * __autoreleasing *)error; +- (void) finishedWritingWithError:(NSError * __autoreleasing *)error; + + @end diff --git a/Objective-Zip/OZZipWriteStream.m b/Objective-Zip/OZZipWriteStream.m index 492cb8a..7e11098 100644 --- a/Objective-Zip/OZZipWriteStream.m +++ b/Objective-Zip/OZZipWriteStream.m @@ -75,18 +75,34 @@ - (void) writeData:(NSData *)data { int err= zipWriteInFileInZip(_zipFile, [data bytes], (uInt) [data length]); - if (err < 0) { - NSString *reason= [NSString stringWithFormat:@"Error writing '%@' in the zipfile", _fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err < 0) + @throw [OZZipException zipExceptionWithError:err reason:@"Error writing '%@' in the zipfile", _fileNameInZip]; } - (void) finishedWriting { int err= zipCloseFileInZip(_zipFile); - if (err != ZIP_OK) { - NSString *reason= [NSString stringWithFormat:@"Error closing '%@' in the zipfile", _fileNameInZip]; - @throw [[OZZipException alloc] initWithError:err reason:reason]; - } + if (err != ZIP_OK) + @throw [OZZipException zipExceptionWithError:err reason:@"Error closing '%@' in the zipfile", _fileNameInZip]; +} + + +#pragma mark - +#pragma mark Writing data (NSError variants) + +- (void) writeData:(NSData *)data error:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + [self writeData:data]; + + } ERROR_WRAP_END(error); +} + +- (void) finishedWritingWithError:(NSError * __autoreleasing *)error { + ERROR_WRAP_BEGIN { + + [self finishedWriting]; + + } ERROR_WRAP_END(error); } diff --git a/Objective_ZipViewController.xib b/Objective_ZipViewController.xib index f2507fc..eec37aa 100644 --- a/Objective_ZipViewController.xib +++ b/Objective_ZipViewController.xib @@ -74,6 +74,26 @@ 16 + + + 292 + {{20, 196}, {280, 37}} + + + + NO + NO + IBCocoaTouchFramework + 0 + 0 + 1 + + + Error Wrapping + + + + 292 @@ -120,7 +140,7 @@ {{20, 152}, {280, 37}} - + NO NO IBCocoaTouchFramework @@ -137,7 +157,7 @@ 292 - {{20, 196}, {280, 244}} + {{20, 243}, {280, 197}} @@ -152,9 +172,10 @@ 274 - {{20, 196}, {280, 244}} + {{20, 243}, {280, 197}} + NO YES YES @@ -184,7 +205,7 @@ {{0, 20}, {320, 460}} - + NO @@ -248,6 +269,15 @@ 23 + + + errorWrapping + + + 7 + + 26 + @@ -275,9 +305,10 @@ - + + @@ -312,6 +343,11 @@ + + 24 + + + @@ -323,6 +359,7 @@ com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -332,7 +369,7 @@ - 23 + 26 @@ -340,12 +377,17 @@ Objective_ZipViewController UIViewController + id id id id id + + errorWrapping + id + zipCheck1 id @@ -382,12 +424,17 @@ Objective_ZipViewController + id id id id id + + errorWrapping + id + zipCheck1 id