BW4 BW4 - 1 month ago 15
iOS Question

Objective-Zip cannot open Zip

I have problem with Objective-Zip. It throw exeption when validate my zip. I checked file it's fine, no problems with unzip/zip. What's more I try zip my files with system default archiver and other.

I use

ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"textPack.zip" mode:ZipFileModeUnzip];


Validate method

- (id) initWithFileName:(NSString *)fileName mode:(ZipFileMode)mode {
if (self= [super init]) {
_fileName= [fileName retain];
_mode= mode;

switch (mode) {
case ZipFileModeUnzip:
_unzFile= unzOpen([_fileName cStringUsingEncoding:NSUTF8StringEncoding]);
if (_unzFile == NULL) {
NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName];
@throw [[[ZipException alloc] initWithReason:reason] autorelease];
}
break;

case ZipFileModeCreate:
_zipFile= zipOpen([_fileName cStringUsingEncoding:NSUTF8StringEncoding], APPEND_STATUS_CREATE);
if (_zipFile == NULL) {
NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName];
@throw [[[ZipException alloc] initWithReason:reason] autorelease];
}
break;

case ZipFileModeAppend:
_zipFile= zipOpen([_fileName cStringUsingEncoding:NSUTF8StringEncoding], APPEND_STATUS_ADDINZIP);
if (_zipFile == NULL) {
NSString *reason= [NSString stringWithFormat:@"Can't open '%@'", _fileName];
@throw [[[ZipException alloc] initWithReason:reason] autorelease];
}
break;

default: {
NSString *reason= [NSString stringWithFormat:@"Unknown mode %d", _mode];
@throw [[[ZipException alloc] initWithReason:reason] autorelease];
}
}
}

return self;
}


Any advice?

FD_ FD_
Answer
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:@"textPack.zip" mode:ZipFileModeUnzip];

Won't work because @"textPack.zip" is no valid file. The "FileName" has to include the path. I think they used a misleading name here.

Use this if your file is from the main bundle:

NSString *path=[[NSBundle mainBundle] pathForResource:@"textPack" ofType:@"zip"];
ZipFile *unzipFile= [[ZipFile alloc] initWithFileName:path mode:ZipFileModeUnzip];

Hope this helps