Index: utils/OFZIP.m ================================================================== --- utils/OFZIP.m +++ utils/OFZIP.m @@ -11,10 +11,12 @@ * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ + +#include #import "OFApplication.h" #import "OFArray.h" #import "OFDate.h" #import "OFDictionary.h" @@ -22,10 +24,14 @@ #import "OFOptionsParser.h" #import "OFSet.h" #import "OFStdIOStream.h" #import "OFZIPArchive.h" #import "OFZIPArchiveEntry.h" + +#import "OFCreateDirectoryFailedException.h" +#import "OFInvalidFormatException.h" +#import "OFOpenFileFailedException.h" #import "autorelease.h" #import "macros.h" #define BUFFER_SIZE 4096 @@ -34,10 +40,11 @@ { int_fast8_t _override, _outputLevel; int _exitStatus; } +- (OFZIPArchive*)openArchiveWithPath: (OFString*)path; - (void)listFilesInArchive: (OFZIPArchive*)archive; - (void)extractFiles: (OFArray*)files fromArchive: (OFZIPArchive*)archive; @end @@ -112,11 +119,11 @@ switch (mode) { case 'l': if ([remainingArguments count] != 1) help(of_stderr, false, 1); - archive = [OFZIPArchive archiveWithPath: + archive = [self openArchiveWithPath: [remainingArguments firstObject]]; [self listFilesInArchive: archive]; break; case 'x': @@ -123,23 +130,55 @@ if ([remainingArguments count] < 1) help(of_stderr, false, 1); files = [remainingArguments objectsInRange: of_range(1, [remainingArguments count] - 1)]; - archive = [OFZIPArchive archiveWithPath: + archive = [self openArchiveWithPath: [remainingArguments firstObject]]; - [self extractFiles: files - fromArchive: archive]; + @try { + [self extractFiles: files + fromArchive: archive]; + } @catch (OFCreateDirectoryFailedException *e) { + [of_stderr writeFormat: + @"\rFailed to create directory %@: %s\n", + [e path], strerror([e errNo])]; + _exitStatus = 1; + } @catch (OFOpenFileFailedException *e) { + [of_stderr writeFormat: + @"\rFailed to open file %@: %s\n", + [e path], strerror([e errNo])]; + _exitStatus = 1; + } + break; default: help(of_stderr, true, 1); break; } [OFApplication terminateWithStatus: _exitStatus]; } + +- (OFZIPArchive*)openArchiveWithPath: (OFString*)path +{ + OFZIPArchive *archive; + + @try { + archive = [OFZIPArchive archiveWithPath: path]; + } @catch (OFOpenFileFailedException *e) { + [of_stderr writeFormat: @"Failed to open file %@: %s\n", + [e path], strerror([e errNo])]; + [OFApplication terminateWithStatus: 1]; + } @catch (OFInvalidFormatException *e) { + [of_stderr writeFormat: @"File %@ is not a valid archive!\n", + path]; + [OFApplication terminateWithStatus: 1]; + } + + return archive; +} - (void)listFilesInArchive: (OFZIPArchive*)archive { OFEnumerator *enumerator = [[archive entries] objectEnumerator]; OFZIPArchiveEntry *entry;