Unzip the file in cocoa

I have a zipped file that I want to extract from it. What is the exact procedure that I have to do to achieve it. Is there any environment for unpacking files in cocoa framework or object C.

+4
source share
6 answers

If you just want to unzip the file, I would recommend using NSTask to call unzip (1). It is probably useful to copy the file to a directory you manage — perhaps in / tmp — before unpacking.

+2
source

On Mac, you can use the unzip built-in command line tool using NSTask :

 - (void) unzip { NSFileManager* fm = [NSFileManager defaultManager]; NSString* zipPath = @"myFile.zip"; NSString* targetFolder = @"/tmp/unzipped"; //this it the parent folder //where your zip content //goes to (must exist) //create a new empty folder (unzipping will fail if any //of the payload files already exist at the target location) [fm createDirectoryAtPath:targetFolder withIntermediateDirectories:NO attributes:nil error:NULL]; //now create a unzip-task NSArray *arguments = [NSArray arrayWithObject:zipPath]; NSTask *unzipTask = [[NSTask alloc] init]; [unzipTask setLaunchPath:@"/usr/bin/unzip"]; [unzipTask setCurrentDirectoryPath:targetFolder]; [unzipTask setArguments:arguments]; [unzipTask launch]; [unzipTask waitUntilExit]; //remove this to start the task concurrently } 

This is a quick and dirty solution. In real life, you probably want to do more error checking and look at the unzip manpage for fancy arguments.

+6
source

If you are using iOS or do not want to use NSTask or something else, I recommend my SSZipArchive library.

Using:

 NSString *path = @"path_to_your_zip_file"; NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped"; [SSZipArchive unzipFileAtPath:path toDestination:destination]; 

Pretty simple.

+5
source
+1
source

Here's a more concise version based on the codingFriend1 approach

 + (void)unzip { NSString *targetFolder = @"/tmp/unzipped"; NSString* zipPath = @"/path/to/my.zip"; NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" arguments:@[@"-o", zipPath, @"-d", targetFolder]]; [task waitUntilExit]; } 

-d specifies the destination directory to be created by unpacking if it does not exist

-o says unzip to overwrite existing files (but not to delete obsolete files, so keep in mind)

There are no validation errors and more, but it is a simple and quick solution.

0
source

-openFile: ( NSWorkspace ) is the easiest way I know.

0
source

Source: https://habr.com/ru/post/1301829/


All Articles