3

I am creating a application to unzip zip files without prompting. I am using MAC OS 10.5 image in Vmaware.

I am new in cocoa programing. I searched few codes regarding this, but its not working..I am not getting any error or warning message.. Please kindly tell me its correct solution.. Zip file is not extracting and my application is closing after some time. I want to Unzip file without prompting and using any third party archive application.

I want to Unzip my file on the same location of its ZIP file.

Here is my code:

/* Assumes sourcePath and targetPath are both
   valid, standardized paths. */

// Create the zip task
NSTask * backupTask = [[NSTask alloc] init];
[backupTask setLaunchPath:@"/usr/bin/ditto"];
[backupTask setArguments:
    [NSArray arrayWithObjects:@"-c", @"-k", @"-X", @"--rsrc", 
    @"~/Desktop/demos.zip", @"~/Desktop", nil]];

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

// Handle the task's termination status
if ([backupTask terminationStatus] != 0)
    NSLog(@"Sorry, didn't work.");

// You *did* remember to wash behind your ears ...
// ... right?
[backupTask release];

-Thanks -with regards !

t3rmin4t0r
  • 45
  • 6

2 Answers2

7

I took your code and changed some of the options. You have specify the full path to everything.

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Need to use the full path to everything
// In this example, I am using my Downloads directory
NSString *destination = [@"~/Downloads" stringByExpandingTildeInPath];
NSString *zipFile = [@"~/Downloads/demos.zip" stringByExpandingTildeInPath];


NSTask *unzip = [[NSTask alloc] init];
[unzip setLaunchPath:@"/usr/bin/unzip"];
[unzip setArguments:[NSArray arrayWithObjects:@"-u", @"-d", 
                     destination, zipFile, nil]];

NSPipe *aPipe = [[NSPipe alloc] init];
[unzip setStandardOutput:aPipe];

[unzip launch];
[unzip waitUntilExit];
[unzip release];


// You can get rid of all the NSLog once you have finish testing
NSData *outputData = [[aPipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];

NSLog(@"Zip File: %@", zipFile);
NSLog(@"Destination: %@", destination);
NSLog(@"Pipe: %@", outputString);
NSLog(@"------------- Finish -----------");
[outputString release];


[pool release];
return 0;
}
Black Frog
  • 11,595
  • 1
  • 35
  • 66
0

Not sure if this is your only issue, but you need to expand the tildes (~) in your path. Normally this is done by the shell, but if you're not using the shell, you have to do it yourself. Luckily, there's an NSString method for doing just that, so you can do [@"~/Desktop/demos.zip" stringByExpandingTildeInPath] and [@"~/Desktop" stringByExpandingTildeInPath].

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Thanks mipadi, I tried your suggestion but it still not working.. File is not Unzipping. Any other suggestion would you or anybody like to prefer? Thanks in advance..-regards – t3rmin4t0r Apr 01 '11 at 05:10