Created
February 15, 2018 18:32
-
-
Save antekone/2aabfbf22d789441143a2d5cae7de3b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <archive.h> | |
#include <archive_entry.h> | |
#define LOG(...) { printf(__VA_ARGS__); puts(""); } | |
int main() { | |
const char* file_name = "test.pkg"; | |
struct archive* a = archive_read_new(); | |
archive_read_support_filter_gzip(a); | |
archive_read_support_filter_bzip2(a); | |
archive_read_support_format_xar(a); | |
if(ARCHIVE_OK != archive_read_open_filename(a, file_name, 10240)) { | |
LOG("can't open file %s", file_name); | |
return 1; | |
} | |
struct archive_entry* entry = NULL; | |
int ret; | |
for(;;) { | |
ret = archive_read_next_header(a, &entry); | |
if(ret == ARCHIVE_EOF) { | |
LOG("encountered EOF"); | |
break; | |
} else if(ret != ARCHIVE_OK) { | |
LOG("archive_read_next_header() failed"); | |
return 1; | |
} | |
LOG("- %s", archive_entry_pathname(entry)); | |
la_int64_t size = archive_entry_size(entry); | |
uint8_t *buff = (uint8_t*) malloc(size); | |
ssize_t r = archive_read_data(a, buff, size); | |
free(buff); | |
if(r < 0) { | |
LOG("archive_read_data() error: %d", (int) r); | |
return 1; | |
} else { | |
LOG("read %zu bytes", r); | |
} | |
} | |
if(ARCHIVE_OK != archive_read_close(a)) { | |
LOG("error while closing archive"); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment