Reputation: 498
i am using iOS-libarchive library for .tar extraction on iphone. i am using the sample code given here at this link http://code.google.com/p/libarchive/wiki/Examples but i am facing problem in extracting .tar file. i am placing my .tar file in library/simulator.../Application/617C31E0/Documents folder.
when i run application for extraction then it completes it flow successfully without giving any error code. but i could not fine that extracted folder anywhere in machine.
here is the piece of code, i am using in my application
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;
}
Please help me into this :(.
Thanks!!
Upvotes: 2
Views: 2632
Reputation: 3485
I write another library that do exactly what you want: https://github.com/mhausherr/Light-Untar-for-iOS
In few words i've only implemented the minimal functions to untar a file. Full explanation here http://blog.octo.com/en/untar-on-ios-the-pragmatic-way/
Upvotes: 3