Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* this is the 'main' method of my "download & unarchive" operation. */
- - (void)main
- {
- struct archive *archive;
- int result;
- // kick off downloading the archive file from my web site
- [self beginDownload];
- // initialize the libarchive reading structures
- result = [self initArchive:&archive];
- // here's the loop that both signals for progress updates and deals with next entries
- while (result == ARCHIVE_OK && ![self isCancelled])
- {
- [self updateProgressForArchive:archive];
- result = [self processNextArchiveEntry:archive];
- }
- // all good loops must come to an end
- if (result == ARCHIVE_EOF)
- {
- result = archive_read_finish(archive);
- }
- // this is somewhat lame, but we _are_ in an NSOperation so it's polite
- if ([self isCancelled])
- {
- NSLog(@"MyDownloadOperation was canceled");
- [downloadThread cancel];
- [[pipe fileHandleForReading] readDataToEndOfFile]; // Unblock the download thread
- result = ARCHIVE_RETRY;
- }
- // everything worked!
- if (result == ARCHIVE_OK)
- {
- [self moveContentFromStagingAreaToFinalArea]; // this is how my app rolls; yours may be different
- }
- else
- {
- [self removeTempDirectory]; // this is how my app rolls; yours may be different
- }
- }
- - (void)beginDownload
- {
- pipe = [NSPipe new];
- downloadThread = [[MyDownloadThread alloc] initWithURL:myDownloadUrl stream:[pipe fileHandleForWriting]];
- [downloadThread start];
- }
- - (int)initArchive:(struct archive **)outArchive
- {
- struct archive *archive = NULL;
- int result = ARCHIVE_OK;
- archive = archive_read_new();
- archive_read_support_compression_bzip2(archive);
- archive_read_support_compression_gzip(archive);
- archive_read_support_format_tar(archive);
- archive_read_support_format_gnutar(archive);
- result = archive_read_open_fd(archive, [[pipe fileHandleForReading] fileDescriptor], getpagesize());
- if(result == ARCHIVE_OK && outArchive != NULL)
- {
- *outArchive = archive;
- }
- lastArchivePosition = 0;
- return result;
- }
Add Comment
Please, Sign In to add comment