Skip to content

Instantly share code, notes, and snippets.

@vortec
Created April 13, 2013 13:52
Show Gist options
  • Select an option

  • Save vortec/5378491 to your computer and use it in GitHub Desktop.

Select an option

Save vortec/5378491 to your computer and use it in GitHub Desktop.
How to recursively unzip a file in Objective C using ZipZap. self.course_data is a NSData object that holds the zip file, coming from a NSURLConnection callback. This is stuff you can ignore.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.progressLabel.text = @"Unpacking...";
connection = nil;
// Save to disk, release memory
NSString *temp_file_path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/temp.zip"];
[self.course_data writeToFile:temp_file_path atomically:YES];
self.course_data = nil;
// Unzip
NSFileManager *file_manager = [NSFileManager defaultManager];
ZZArchive *archive = [ZZArchive archiveWithContentsOfURL:[NSURL fileURLWithPath:temp_file_path]];
for (NSUInteger i=0, count=archive.entries.count; i<count; i++) {
ZZArchiveEntry* archiveEntry = archive.entries[i];
NSString *unzip_target_path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:archiveEntry.fileName];
NSLog(@"Target: %@", unzip_target_path);
if (archiveEntry.fileMode & S_IFDIR) { // check if directory bit is set
NSLog(@"Creating folder.");
[file_manager createDirectoryAtPath:unzip_target_path withIntermediateDirectories:NO attributes:nil error:nil];
} else {
NSLog(@"no folder maybe?");
[archiveEntry.data writeToFile:unzip_target_path atomically:YES];
}
NSLog(@"---");
}
self.progressLabel.text = @"Finished.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment