问题
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];
回答1:
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?
回答2:
Why can't you embed the quote marks?
NSString *quotedPath = [NSString stringWithFormat:@"\"%@\"", path];
NSArray *arguments = [NSArray arrayWithObjects:zipPath, @"-d", quotedPath, nil];
回答3:
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?
来源:https://stackoverflow.com/questions/5211737/nstask-question