Problematic NSTask issue

  • Hello all,
    My application calls for the ability to zip and unzip a file; research
    has indicated that using NSTask to call either zip or ditto from the
    command line is the best way to go. My code currently looks something
    like this:

            NSTask *backupTask = [[NSTask alloc] init];
    [backupTask setLaunchPath:@"/usr/bin/zip"];
    [backupTask setArguments:
        [NSArray arrayWithObjects:@"-r", @"-j", finalFilename, file, nil]];

    // Launch it and wait for execution
    [backupTask launch];
    [backupTask waitUntilExit];

    This works just fine! I end up with a zipped file, and it works great.
    But then, I want to unzip it. Here's my code:

    NSTask *backupTask = [[NSTask alloc] init];
    [backupTask setLaunchPath:@"/usr/bin/unzip"];
    [backupTask setArguments:
        [NSArray arrayWithObjects:file, nil]];

    // Launch it and wait for execution
    [backupTask launch];
    [backupTask waitUntilExit];

    And it errors out with a message from unzip saying it can't find the file:

    unzip:  cannot find or open '/Users/aaron/TestDir/picture.enc',
    '/Users/aaron/TestDir/picture.enc'.zip or
    '/Users/aaron/TestDir/picture.enc'.ZIP.

    '/Users/aaron/TestDir/picture.enc' is the file it should be unzipping.
    It's there, I swear! And it's a zipped file; I can change its
    extension in the Finder to .zip and double-click it, no problem.

    I have also tried using the same unzip command as NSTask would produce
    on the command line, in this case:

    $ unzip '/Users/aaron/TestDir/picture.enc'

    And it works as well! So there's something screwy, it seems, with the
    way NSTask is executing this application? Are there other suggestions
    for how to troubleshoot this problem?

    Thanks in advance!
    Aaron.

    P.S. I've also tried the above code, exchanging zip for ditto (and
    entering the requisite different arguments). Essentially it's the same
    problem; ditto can't find the file to unzip either!

    --
    Aaron Vegh, Principal
    Innoveghtive Inc.
    P: (647) 477-2690
    C: (905) 924-1220
    www.innoveghtive.com
    www.website-in-a-day.com
  • On Dec 4, 2007, at 16:37, Aaron Vegh wrote:

    > But then, I want to unzip it. Here's my code:
    >
    > NSTask *backupTask = [[NSTask alloc] init];
    > [backupTask setLaunchPath:@"/usr/bin/unzip"];
    > [backupTask setArguments:
    > [NSArray arrayWithObjects:file, nil]];
    >
    > // Launch it and wait for execution
    > [backupTask launch];
    > [backupTask waitUntilExit];
    >
    > And it errors out with a message from unzip saying it can't find
    > the file:
    >
    > unzip:  cannot find or open '/Users/aaron/TestDir/picture.enc',
    > '/Users/aaron/TestDir/picture.enc'.zip or
    > '/Users/aaron/TestDir/picture.enc'.ZIP.
    >
    > '/Users/aaron/TestDir/picture.enc' is the file it should be unzipping.
    > It's there, I swear! And it's a zipped file; I can change its
    > extension in the Finder to .zip and double-click it, no problem.

    Is your "file" argument a quoted string?

    If you are trying to send the argument "/Foo Bar Baz", you should use
    @"/Foo Bar Baz". @"'/Foo Bar Baz'" is a different file. NSTask is not
    a shell, you do not need to quote stuff because it does this for you
    if needed.

    Best Regards,

    Nir Soffer
  • Hi Nir

    > Is your "file" argument a quoted string?

    Here, it is. However, I just added that functionality as a means of
    trying to find a solution to the problem. I'll remove that, but here's
    the error I got before I did so:

    unzip:  cannot find zipfile directory in one of
    /Users/aaron/TestDir/Picture 1.enc or
            /Users/aaron/TestDir/Picture 1.enc.zip, and cannot find
    /Users/aaron/TestDir/Picture 1.enc.ZIP, period.

    Again, the first guess there is actually where the file is in this case.

    Thanks!
    Aaron.
  • Hi Mark,
    Thanks for writing back, fellow GTA-er!

    I've tried the same command as you in the Terminal, and get the same
    response. However, when you feed unzip a directory path, you see my
    error. Try this:

    aeneas:~ aaron$ unzip foo/bar
    unzip:  cannot find or open foo/bar, foo/bar.zip or foo/bar.ZIP.

    I'm running Leopard. Checking the version:

    aeneas:~ aaron$ unzip -v
    UnZip 5.52 of 28 February 2005, by Info-ZIP.

    Cheers!
    Aaron.

    On Dec 4, 2007 2:34 PM, Mark Ritchie <mark.ritchie...> wrote:
    >
    >
    > On 4-Dec-07, at 1:51 PM, Aaron Vegh wrote:
    >
    >
    > unzip:  cannot find zipfile directory in one of
    >
    > /Users/aaron/TestDir/Picture 1.enc or
    >
    > /Users/aaron/TestDir/Picture 1.enc.zip, and cannot find
    >
    > /Users/aaron/TestDir/Picture 1.enc.ZIP, period.
    > Hey Aaron,
    >
    > Playing devil's advocate here for a moment... :-)
    > Are you sure that that you're calling the standard unzip?
    >
    > The error message that I see for a non-existent file is slightly different:
    > Marks-Macbook:~ mark$ unzip foo
    > unzip:  cannot find or open foo, foo.zip or foo.ZIP.
    > (Mac OS X 10.4.10 on Intel.)
    >
    > Good luck!
    > Mark
    >
    >
    > __
    > Mark Ritchie
    > Cell 416-843-5479
    >
    > Cocoa and WebObjects Developer
    > Diamond Lake Consulting Inc.
    > Toronto, Ontario, Canada
    >
    >
    >

    --
    Aaron Vegh, Principal
    Innoveghtive Inc.
    P: (647) 477-2690
    C: (905) 924-1220
    www.innoveghtive.com
    www.website-in-a-day.com
  • On Dec 4, 2007, at 9:37 AM, Aaron Vegh wrote:

    > [...]
    > And it errors out with a message from unzip saying it can't find the
    > file:
    >
    > unzip:  cannot find or open '/Users/aaron/TestDir/picture.enc',
    > '/Users/aaron/TestDir/picture.enc'.zip or
    > '/Users/aaron/TestDir/picture.enc'.ZIP.
    >
    > '/Users/aaron/TestDir/picture.enc' is the file it should be unzipping.
    > It's there, I swear! And it's a zipped file; I can change its
    > extension in the Finder to .zip and double-click it, no problem.

    Just judging from the error message, the argument being passed
    includes the single quotes.

    E.g., $ unzip foo
    produces
    unzip:  cannot find or open foo, foo.zip or foo.ZIP.

    so the fact that the error message includes the single quotes in the
    filename is very suggestive.

    What does NSLog("file: %@", file); produce at the point of launch?

    Cheers!
    --Chris Ryland / Em Software, Inc. / www.emsoftware.com
  • Hi Chris,
    Thanks for writing.

    > What does NSLog("file: %@", file); produce at the point of launch?

    I have updated my code to remove those red herring quotes, and here's
    my current result, including the filename prior to the NSTask:

    2007-12-04 14:51:26.265 Quantum2[80743:10b] file is
    /Users/aaron/TestDir/picture.enc
    unzip:  cannot find zipfile directory in one of
    /Users/aaron/TestDir/picture.enc or
            /Users/aaron/TestDir/picture.enc.zip, and cannot find
    /Users/aaron/TestDir/picture.enc.ZIP, period.

    Note that the first line is the NSLog that you've asked about.

    A real head-scratcher!

    Cheers,
    Aaron.
  • Hi there,

    > I think you need to add some parameters to define an output path to unzip (-d path),or use the setCurrentDirectoryPath message.

    Okay, I tried that, but same result. Actually, I'm starting to see
    something new here. Here's my current output, and note the new stanza
    from this "Archive:" process.

    2007-12-04 15:39:43.054 Quantum2[84111:10b] file is
    /Users/aaron/TestDir/sample.enc
    Archive:  /Users/aaron/TestDir/sample.enc
    End-of-central-directory signature not found.  Either this file is not
    a zipfile, or it constitutes one disk of a multi-part archive.  In the
    latter case the central directory and zipfile comment will be found on
    the last disk(s) of this archive.
    unzip:  cannot find zipfile directory in one of
    /Users/aaron/TestDir/sample.enc or
          /Users/aaron/TestDir/sample.enc.zip, and cannot find
    /Users/aaron/TestDir/sample.enc.ZIP, period.

    A suggestion was made that the output looks a little funky; perhaps
    there's some weird line breaks? I don't know how to check for that.

    Cheers,
    Aaron.
  • Hi Chris,

    > No, what I'm saying is that you have a newline character (\r or \n) as
    > the first character in your filename, before /Users/aaron/...
    >
    > That's what your logfile is showing, and what's what zip is showing.
    >
    > It's there; gotta figure out why.

    I'm pretty sure now that the newline isn't in my code; it's in the
    return from unzip. When I run the command from the Terminal, I get the
    same formatted output:

    unzip:  cannot find zipfile directory in one of sample.enc or
              sample.enc.zip, and cannot find sample.enc.ZIP, period.

    I was tipped off by noticing that the second line is perfectly lined
    up with the start of the message on line 1. And I've spent an hour or
    so exhausting the means of finding a \n character in my string!

    I'm running out of ideas here... :-P

    Thanks,
    Aaron.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today