2

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];
Pripyat
  • 2,937
  • 2
  • 35
  • 69

3 Answers3

2

Why can't you embed the quote marks?

NSString *quotedPath = [NSString stringWithFormat:@"\"%@\"", path];
NSArray *arguments = [NSArray arrayWithObjects:zipPath, @"-d", quotedPath, nil];
Richard
  • 3,316
  • 30
  • 41
  • that still won't work it seems. The path I have is: `/var/folders/vH/vHs82ahGF3KV5fB5omJRfE+++TI/-Tmp-/Test App.zip`, but the console outputs `unzip: cannot find or open /var/folders/vH/vHs82ahGF3KV5fB5omJRfE+++TI/-Tmp-/Test App.zip, /var/folders/vH/vHs82ahGF3KV5fB5omJRfE+++TI/-Tmp-/Test App.zip.zip or /var/folders/vH/vHs82ahGF3KV5fB5omJRfE+++TI/-Tmp-/Test App.zip.ZIP.` – Pripyat Mar 06 '11 at 16:35
  • so really, the problem has nothing to do with "because I'm storing the zip file in a temporary folder" but because there's a space in the zip file's name. You could try `stringByAddingPercentEscapesUsingEncoding:`; not sure how that works on local filesystems. – Richard Mar 06 '11 at 16:52
  • Yes it does - as you may know, spaces in Terminal will cause a term to be seen as two different things, hence my assumption. – Pripyat Mar 06 '11 at 17:14
  • 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? – CRD Mar 06 '11 at 18:03
  • The file was deleted a split second before - that was the problem. Please post this comment as an answer so I can mark it as the right one. – Pripyat Mar 06 '11 at 18:45
0

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?

Francis McGrew
  • 7,264
  • 1
  • 33
  • 30
  • No, it doesn't have to go as complicated as that. Turns out, the input doesn't need `"` signs, just a normal string. – Pripyat Mar 06 '11 at 18:45
0

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?

CRD
  • 52,522
  • 5
  • 70
  • 86