I have a zipped file, which i want to extract the contents of it. What is the exact procedure that i should do to achieve it. Is there any framework to unzip the files in cocoa framework or objective C.
If you are on iOS or don't want to use NSTask
or whatever, I recommend my library SSZipArchive.
Usage:
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.
On the Mac you can use the built in unzip
command line tool using an NSTask
:
- (void) unzip {
NSFileManager* fm = [NSFileManager defaultManager];
NSString* zipPath = @"myFile.zip";
NSString* targetFolder = @"/tmp/unzipped"; //this it the parent folder
//where your zip's 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
}
That is a quick and dirty solution. In real life you will probably want to do more error checking and have a look at the unzip
manpage for fancy arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With