Question

I want to unzip the file via terminal on Mac instead of using ZipArchive or SSZipArchive.

In terminal, I tried "unzip" command and it works good, but I do not how to express via objective c code.

I've tried this way (link: Unzip without prompt) It works but only unzipped half of my files instead of all the files.

Thanks !!

Was it helpful?

Solution

Have you tried the system() function?

system("unzip -u -d [destination full path] [zip file full path]");

You'll need to construct an NSString with your full command (including the file paths), and turn it into a C string for the system command, something like this:

NSString *myCommandString = 
[NSString stringWithFormat:@"unzip -u -d %@ %@", destinationPath, zipPath];
system([myCommandString UTF8String]);

This won't return any of the command's output, so you'd be better off with the solution from the Unzip without prompt question if you want details about how the operation went, but if your project doesn't need error-handling this should be fine.

OTHER TIPS

See the following. I've revised a bit.

- (void)unzipme {
    NSTask *task = [[NSTask alloc] init];
    NSMutableString *command = [[NSMutableString alloc] initWithString:@""];
    NSArray *args;
    [task setLaunchPath:@"/bin/sh"];
    [command appendString:@"unzip "];
    [command appendString:[self convertShell:sourcePath];
    [command appendString:@" "];
    [command appendString:-d ];
    [command appendString:[self convertShell:[self exportPath]]];
    args = [NSArray arrayWithObjects:@"-c",command,nil]; // Line 10
    [task setArguments:args];
    NSPipe *pipe1;
    pipe1 = [NSPipe pipe];
    [task setStandardOutput: pipe1];
    [task launch];
    [task waitUntilExit];
}

- (NSString *)convertShell: (NSString *)path {
    static NSString *chr92 = @"\\";
    NSMutableString *replace = [[NSMutableString alloc]initWithString:chr92];
    [replace appendString:@" "];
    NSString *sPath = [self Replace:path :@" " :replace];
    return sPath;
}

convertShell converts the Objective-C path to Shell path. Moreover, according to the unzip Man Page, this command-line tool takes a switch (-d) to specify a directory where to unzip an archive. sourcePath is a source zip file to unzip. exportPath is a destination folder. If you get an error, insert NSLog(@"%@",command); before Line 10 and show me what the command says.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top