libtar is small but effective library, although it has some bugs when dealing with long path.
The Steps are following:
download libtar-1.2.11 and uncompress it, the main path is /path/libtar-1.2.11
build with command “./configure && make”, if you want to debug, add “-g” for gcc to build.
make “test” folder “/path/libtar-1.2.11/test”. find header file and libtar.a file
put 1.c in “/path/libtar-1.2.11/test”
so finally, the build command will be “g++ 1.c -o 1 ../lib/libtar.a -I ../bin/libtar-1.2.11/include”
some important code, for example, char* subfile1 = “./sub1/1.txt”
this is for file under sub folder.
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 | #include <tar.h> #include <libtar.h> #include <iostream> #include <fcntl.h> using namespace std; int main(){ // tar if(1){ TAR* tar_handle; char* tar_fname = "test.tar"; tar_open(&tar_handle, tar_fname, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU); char* subfile1 = "./sub1/1.txt"; tar_append_file(tar_handle, subfile1, subfile1); char* subfile2 = "test1.c"; tar_append_file(tar_handle, subfile2, subfile2); tar_close(tar_handle); } // untar if(0){ TAR* tar_handle; char* tar_fname = "test.tar"; tar_open(&tar_handle, tar_fname, NULL, O_RDONLY, 0644, TAR_GNU); char* savefold = "temp"; tar_extract_all(tar_handle, "temp"); tar_close(tar_handle); } return 0; } |