Guest User

Untitled

a guest
Jul 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. /* this is the 'main' method of my "download & unarchive" operation. */
  2. - (void)main
  3. {
  4. struct archive *archive;
  5. int result;
  6.  
  7. // kick off downloading the archive file from my web site
  8. [self beginDownload];
  9.  
  10. // initialize the libarchive reading structures
  11. result = [self initArchive:&archive];
  12.  
  13. // here's the loop that both signals for progress updates and deals with next entries
  14. while (result == ARCHIVE_OK && ![self isCancelled])
  15. {
  16. [self updateProgressForArchive:archive];
  17. result = [self processNextArchiveEntry:archive];
  18. }
  19.  
  20. // all good loops must come to an end
  21. if (result == ARCHIVE_EOF)
  22. {
  23. result = archive_read_finish(archive);
  24. }
  25.  
  26. // this is somewhat lame, but we _are_ in an NSOperation so it's polite
  27. if ([self isCancelled])
  28. {
  29. NSLog(@"MyDownloadOperation was canceled");
  30. [downloadThread cancel];
  31. [[pipe fileHandleForReading] readDataToEndOfFile]; // Unblock the download thread
  32. result = ARCHIVE_RETRY;
  33. }
  34.  
  35. // everything worked!
  36. if (result == ARCHIVE_OK)
  37. {
  38. [self moveContentFromStagingAreaToFinalArea]; // this is how my app rolls; yours may be different
  39. }
  40. else
  41. {
  42. [self removeTempDirectory]; // this is how my app rolls; yours may be different
  43. }
  44. }
  45.  
  46.  
  47. - (void)beginDownload
  48. {
  49. pipe = [NSPipe new];
  50. downloadThread = [[MyDownloadThread alloc] initWithURL:myDownloadUrl stream:[pipe fileHandleForWriting]];
  51. [downloadThread start];
  52. }
  53.  
  54.  
  55. - (int)initArchive:(struct archive **)outArchive
  56. {
  57. struct archive *archive = NULL;
  58. int result = ARCHIVE_OK;
  59.  
  60. archive = archive_read_new();
  61. archive_read_support_compression_bzip2(archive);
  62. archive_read_support_compression_gzip(archive);
  63. archive_read_support_format_tar(archive);
  64. archive_read_support_format_gnutar(archive);
  65.  
  66. result = archive_read_open_fd(archive, [[pipe fileHandleForReading] fileDescriptor], getpagesize());
  67.  
  68. if(result == ARCHIVE_OK && outArchive != NULL)
  69. {
  70. *outArchive = archive;
  71. }
  72.  
  73. lastArchivePosition = 0;
  74.  
  75. return result;
  76. }
Add Comment
Please, Sign In to add comment