NSTask Question

三世轮回 提交于 2019-12-08 03:00:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!