Pripyat
Pripyat

Reputation: 2937

NSTask Question

I'm trying to get my NSTask to unzip a file for me. This works fine if the path has no spaces, but when it does, it can't find any of the files. I can't hardcode the " signs because I'm storing the zip file in a temporary folder, which is assigned by the system.

Does anyone know how to achieve this?

Here's my code:

NSTask*task = [[NSTask alloc] init];

[task setLaunchPath:@"/usr/bin/unzip"];

NSArray*arguments = [NSArray arrayWithObjects:zipPath,@"-d",path,nil];

[task setArguments:arguments];

[task launch];

[task release];

Upvotes: 2

Views: 1172

Answers (3)

CRD
CRD

Reputation: 53000

Having a space in the argument does not look like your problem - note that the console is showing the pathname with a space. An argument with a space is passed as a single argument, I've just confirmed it will happily unzip @"a space.zip". Have you checked the file does exist where you think it does and you have access to it?

Upvotes: 0

Francis McGrew
Francis McGrew

Reputation: 7272

Could you parse the path components using NSString's - (NSArray *)pathComponents method, add the quotes where needed, then rebuild the string using (NSString *)pathWithComponents:(NSArray *)components

Does that work?

Upvotes: 0

Richard
Richard

Reputation: 3386

Why can't you embed the quote marks?

NSString *quotedPath = [NSString stringWithFormat:@"\"%@\"", path];
NSArray *arguments = [NSArray arrayWithObjects:zipPath, @"-d", quotedPath, nil];

Upvotes: 2

Related Questions