Created
October 25, 2015 14:01
-
-
Save gen2brain/c6745855028de8341175 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 <stdlib.h> | |
#include <fcntl.h> | |
#include <archive.h> | |
#include <archive_entry.h> | |
int myclose(struct archive *a, void *client_data); | |
ssize_t myread(struct archive *a, void *client_data, const void **buff); | |
struct mydata { | |
int fd; | |
const char* name; | |
void* buff[10240]; | |
}; | |
void test_archive_read_open(const char *name) { | |
int err; | |
struct archive *a; | |
struct archive_entry *e; | |
struct mydata *mydata; | |
mydata = malloc(sizeof(struct mydata)); | |
a = archive_read_new(); | |
mydata->name = name; | |
mydata->fd = open(mydata->name, O_RDONLY); | |
archive_read_support_filter_all(a); | |
archive_read_support_format_all(a); | |
err = archive_read_open(a, mydata, NULL, myread, myclose); | |
if(err != ARCHIVE_OK) { | |
printf("archive_read_open: %d\n", err); | |
} | |
while((err = archive_read_next_header(a, &e)) != ARCHIVE_EOF) { | |
if(err == ARCHIVE_OK) { | |
printf("%s\n", archive_entry_pathname(e)); | |
} else { | |
printf("archive_read_next_header: %d\n", err); | |
break; | |
} | |
} | |
archive_read_close(a); | |
archive_read_free(a); | |
} | |
ssize_t myread(struct archive *a, void *client_data, const void **buff) { | |
struct mydata *mydata = client_data; | |
*buff = mydata->buff; | |
return (read(mydata->fd, mydata->buff, 10240)); | |
} | |
int myclose(struct archive *a, void *client_data) { | |
struct mydata *mydata = client_data; | |
if(mydata->fd > 0) { | |
close(mydata->fd); | |
} | |
free(mydata); | |
return (ARCHIVE_OK); | |
} | |
void test_archive_read_open_filename(const char *name) { | |
int err; | |
struct archive *a; | |
struct archive_entry *e; | |
a = archive_read_new(); | |
archive_read_support_filter_all(a); | |
archive_read_support_format_all(a); | |
err = archive_read_open_filename(a, name, 10240); | |
if(err != ARCHIVE_OK) { | |
printf("archive_read_open: %d\n", err); | |
} | |
while((err = archive_read_next_header(a, &e)) != ARCHIVE_EOF) { | |
if(err == ARCHIVE_OK) { | |
printf("%s\n", archive_entry_pathname(e)); | |
} else { | |
printf("archive_read_next_header: %d\n", err); | |
break; | |
} | |
} | |
archive_read_close(a); | |
archive_read_free(a); | |
} | |
int main(int argc, char *argv[]) { | |
int i; | |
for(i = 1; i < argc; i++) { | |
test_archive_read_open(argv[i]); | |
printf("\n"); | |
test_archive_read_open_filename(argv[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment