NS任务问题
我正在尝试让我的 NSTask
为我解压缩文件。如果路径没有空格,则此方法可以正常工作,但如果路径有空格,则无法找到任何文件。我无法对 "
符号进行硬编码,因为我将 zip 文件存储在系统分配的临时文件夹中。
有谁知道如何实现此目的?
这是我的代码:
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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不能嵌入引号?
Why can't you embed the quote marks?
参数中包含空格看起来不像您的问题 - 请注意,控制台显示带有空格的路径名。带有空格的参数作为单个参数传递,我刚刚确认它会很高兴地解压缩@“a space.zip”。您是否检查过该文件确实存在于您认为存在的位置并且您可以访问它?
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?
您可以使用 NSString 的
- (NSArray *)pathComponents
方法解析路径组件,在需要的地方添加引号,然后使用(NSString *)pathWithComponents:(NSArray *)components
重建字符串代码>这有效吗?
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?