1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <archive.h>
#include <archive_entry.h>
static void tar_create(const char *outname, const char **filename)
{
struct archive* a;
struct archive_entry* entry;
struct stat st;
char buff[8192];
int fd(0);
int len(0);
a = archive_write_new();
archive_write_set_compression_gzip( a );
archive_write_set_format_ustar( a );
archive_write_set_bytes_per_block( a, 4096 );
archive_write_open_filename( a, outname );
while( *filename ) {
stat( *filename, &st );
entry = archive_entry_new();
archive_entry_copy_stat(entry, &st );
archive_entry_set_pathname( entry, *filename );
archive_write_header( a, entry );
fd = open( *filename, O_RDONLY );
if( fd != -1 ) {
len = read( fd, buff, sizeof( buff ));
while( len > 0 ) {
//buf contains the correct data and the len the correct length at this point, but check is 0.
size_t check = archive_write_data( a, buff, len );
len = read(fd, buff, sizeof(buff));
}
close(fd);
}
archive_entry_free(entry);
++filename;
}
archive_write_close(a);
archive_write_finish(a);
}
|