Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* call processNextArchiveEntry in some kind of loop! */
- - (int)processNextArchiveEntry:(struct archive *)archive
- {
- struct archive_entry *entry = NULL;
- int result = ARCHIVE_OK;
- result = archive_read_next_header(archive, &entry);
- if (result == ARCHIVE_OK)
- {
- if (archive_entry_filetype(entry) == AE_IFDIR)
- {
- result = [self processDirectory:entry inArchive:archive];
- }
- else if (archive_entry_filetype(entry) == AE_IFREG)
- {
- result = [self processFile:entry inArchive:archive];
- }
- }
- return result;
- }
- - (int)processDirectory:(struct archive_entry *)entry inArchive:(struct archive *)archive
- {
- NSString *path = nil;
- NSError *error = nil;
- BOOL exists, isDirectory;
- BOOL ok = NO;
- path = [self fullDestinationPathForEntry:entry];
- exists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
- if (!exists)
- {
- ok = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
- }
- else if (exists && isDirectory)
- {
- // we allow users to re-download content, in which case this can happen.
- ok = YES;
- }
- return ok ? ARCHIVE_OK : ARCHIVE_FATAL;
- }
- - (int)processFile:(struct archive_entry *)entry inArchive:(struct archive *)archive
- {
- NSFileHandle *file = nil;
- NSString *path = nil;
- BOOL ok = NO;
- int result = ARCHIVE_FAILED;
- path = [self fullDestinationPathForEntry:entry];
- ok = [fileManager createFileAtPath:path contents:nil attributes:nil];
- if (ok)
- {
- file = [NSFileHandle fileHandleForWritingAtPath:path];
- }
- if(file != nil)
- {
- result = archive_read_data_into_fd(archive, [file fileDescriptor]);
- [file closeFile];
- }
- return result;
- }
- - (NSString *)fullDestinationPathForEntry:(struct archive_entry *)entry
- {
- NSString *path = nil;
- const char *entry_path = NULL;
- entry_path = archive_entry_pathname(entry);
- if (entry_path != NULL)
- {
- path = [fileManager stringWithFileSystemRepresentation:entry_path length:strlen(entry_path)];
- }
- if (path != nil)
- {
- path = [tempDirectory stringByAppendingPathComponent:path];
- }
- return path;
- }
Add Comment
Please, Sign In to add comment