发出使用iOS-libarchive库提取.tar文件的问题

时间:2011-07-20 05:18:02

标签: iphone objective-c ipad tar

我在iphone上使用iOS-libarchive库进行.tar提取。我正在使用此链接中给出的示例代码 http://code.google.com/p/libarchive/wiki/Examples  但我在提取.tar文件时面临问题。 我将.tar文件放在库/模拟器... / Application / 617C31E0 / Documents文件夹中。

当我运行应用程序进行提取时,它会成功完成流程而不会给出任何错误代码。但我无法在机器的任何地方解压缩该文件夹。

这是我在我的应用程序中使用的代码片段

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;  
}

请帮助我:(。

谢谢!

1 个答案:

答案 0 :(得分:3)

我写了另一个完全符合你想要的库:https://github.com/mhausherr/Light-Untar-for-iOS

简言之,我只实现了解压文件的最小函数。 这里有完整的解释http://blog.octo.com/en/untar-on-ios-the-pragmatic-way/