Guest User

Untitled

a guest
Jul 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. /* call processNextArchiveEntry in some kind of loop! */
  2. - (int)processNextArchiveEntry:(struct archive *)archive
  3. {
  4. struct archive_entry *entry = NULL;
  5. int result = ARCHIVE_OK;
  6.  
  7. result = archive_read_next_header(archive, &entry);
  8.  
  9. if (result == ARCHIVE_OK)
  10. {
  11. if (archive_entry_filetype(entry) == AE_IFDIR)
  12. {
  13. result = [self processDirectory:entry inArchive:archive];
  14. }
  15. else if (archive_entry_filetype(entry) == AE_IFREG)
  16. {
  17. result = [self processFile:entry inArchive:archive];
  18. }
  19. }
  20.  
  21. return result;
  22. }
  23.  
  24.  
  25. - (int)processDirectory:(struct archive_entry *)entry inArchive:(struct archive *)archive
  26. {
  27. NSString *path = nil;
  28. NSError *error = nil;
  29. BOOL exists, isDirectory;
  30. BOOL ok = NO;
  31.  
  32. path = [self fullDestinationPathForEntry:entry];
  33. exists = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];
  34.  
  35. if (!exists)
  36. {
  37. ok = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  38. }
  39. else if (exists && isDirectory)
  40. {
  41. // we allow users to re-download content, in which case this can happen.
  42. ok = YES;
  43. }
  44.  
  45. return ok ? ARCHIVE_OK : ARCHIVE_FATAL;
  46. }
  47.  
  48.  
  49. - (int)processFile:(struct archive_entry *)entry inArchive:(struct archive *)archive
  50. {
  51. NSFileHandle *file = nil;
  52. NSString *path = nil;
  53. BOOL ok = NO;
  54. int result = ARCHIVE_FAILED;
  55.  
  56. path = [self fullDestinationPathForEntry:entry];
  57.  
  58. ok = [fileManager createFileAtPath:path contents:nil attributes:nil];
  59.  
  60. if (ok)
  61. {
  62. file = [NSFileHandle fileHandleForWritingAtPath:path];
  63. }
  64.  
  65. if(file != nil)
  66. {
  67. result = archive_read_data_into_fd(archive, [file fileDescriptor]);
  68. [file closeFile];
  69. }
  70.  
  71. return result;
  72. }
  73.  
  74.  
  75. - (NSString *)fullDestinationPathForEntry:(struct archive_entry *)entry
  76. {
  77. NSString *path = nil;
  78. const char *entry_path = NULL;
  79.  
  80. entry_path = archive_entry_pathname(entry);
  81.  
  82. if (entry_path != NULL)
  83. {
  84. path = [fileManager stringWithFileSystemRepresentation:entry_path length:strlen(entry_path)];
  85. }
  86.  
  87. if (path != nil)
  88. {
  89. path = [tempDirectory stringByAppendingPathComponent:path];
  90. }
  91.  
  92. return path;
  93. }
Add Comment
Please, Sign In to add comment