2011-07-20 58 views
2

我在iOS上使用iOS-libarchive庫解壓.tar文件。我正在使用此處鏈接的示例代碼 http://code.google.com/p/libarchive/wiki/Examples 但我在解壓縮.tar文件時遇到問題。 我將我的.tar文件放在庫/模擬器.../Application/617C31E0/Documents文件夾中。使用iOS-libarchive庫解壓縮.tar文件

當我運行應用程序提取然後它完成它成功流程沒有提供任何錯誤代碼。但我無法罰機器中任何地方提取文件夾。

這裏是一段代碼,我用我的應用程序

static int copy_data(struct archive *ar, struct archive *aw) 
{ 
    int r; 
    const void *buff; 
    size_t size; 
    off_t offset; 

    for (;;) { 
     r = archive_read_data_block(ar, &buff, &size, &offset); 
     if (r == ARCHIVE_EOF) 
      return (ARCHIVE_OK); 
     if (r != ARCHIVE_OK) 
      return (r); 
     r = archive_write_data_block(aw, buff, size, offset); 
     if (r != ARCHIVE_OK) { 
      warn("archive_write_data_block()", 
       archive_error_string(aw)); 
      return (r); 
     } 
    } 
} 

static int verbose = 0; 

static void extract(const char *filename, int do_extract, int flags) 
{ 
    struct archive *a; 
    struct archive *ext; 
    struct archive_entry *entry; 
    int r; 

    a = archive_read_new(); 
    ext = archive_write_disk_new(); 
    archive_write_disk_set_options(ext, flags); 
    /* 
    * Note: archive_write_disk_set_standard_lookup() is useful 
    * here, but it requires library routines that can add 500k or 
    * more to a static executable. 
    */ 

    archive_read_support_format_tar(a); 
    /* 
    * On my system, enabling other archive formats adds 20k-30k 
    * each. Enabling gzip decompression adds about 20k. 
    * Enabling bzip2 is more expensive because the libbz2 library 
    * isn't very well factored. 
    */ 

    if (filename != NULL && strcmp(filename, "-") == 0) 
     filename = NULL; 
    if ((r = archive_read_open_file(a, filename, 10240))) 
     fail("archive_read_open_file()", 
      archive_error_string(a), r); 
    for (;;) { 
     r = archive_read_next_header(a, &entry); 
     if (r == ARCHIVE_EOF) 
      break; 
     if (r != ARCHIVE_OK) 
      fail("archive_read_next_header()", 
       archive_error_string(a), 1); 
     if (verbose && do_extract) 
      msg("x "); 
     if (verbose || !do_extract) 
      msg(archive_entry_pathname(entry)); 
     if (do_extract) { 
      r = archive_write_header(ext, entry); 
      if (r != ARCHIVE_OK) 
       warn("archive_write_header()", 
        archive_error_string(ext)); 
      else { 
       copy_data(a, ext); 
       r = archive_write_finish_entry(ext); 
       if (r != ARCHIVE_OK) 
        fail("archive_write_finish_entry()", 
         archive_error_string(ext), 1); 
      } 

     } 
     if (verbose || !do_extract) 
      msg("\n"); 
    } 
    archive_read_close(a); 
    archive_read_finish(a); 
    exit(0); 
} 

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
    // Override point for customization after application launch 
    [window makeKeyAndVisible]; 

    // Extract files from archive into named dir in the temp dir 

    NSString* databaseName = @"PIIS147020451170078X.tar"; 
    NSArray* documentPaths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDir = [documentPaths objectAtIndex:0]; 
    NSString* make7zResPath = [documentsDir stringByAppendingPathComponent:databaseName]; 

    /* 
    NSString *tmpDirname = @"Extract7z"; 
    NSString *make7zFilename = @"PIIS147020451170078X";//@"test.7z"; 
    NSString *make7zResPath = [[NSBundle mainBundle] pathForResource:make7zFilename ofType:@"tar"]; 
    */ 

    extract([make7zResPath cStringUsingEncoding:NSUTF8StringEncoding],1, ARCHIVE_EXTRACT_TIME); 

    return; 
} 

請幫我到這個:(。

謝謝!

+0

你能發佈更多關於你在做什麼的信息嗎?例如什麼是文件路徑被提取?例如,他們是否擁有絕對路徑? –

回答