在c中使用libtar库

iru*_*eru 7 c linux tar

我正在尝试使用c创建一个tar文件.我无法使用的原因

system("tar -cvf xxxx.tar xxxx");

我的代码是:

#include <stdio.h>
#include <libtar.h>
#include <fcntl.h>

int main(void) {

        TAR *pTar;

        char *tarFilename = "file.tar";
        char *srcDir = "directory";

        char *extractTo = ".";
        tar_open(&pTar, tarFilename, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU);
        tar_append_tree(pTar, srcDir, extractTo);
        tar_close(pTar);
        return (0);

}
Run Code Online (Sandbox Code Playgroud)

运行此代码后,当我想解压缩时

tar -xvf file.tar
Run Code Online (Sandbox Code Playgroud)

我收到了一个错误

tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
Run Code Online (Sandbox Code Playgroud)

我的c代码有什么问题?

Pie*_*rre 9

我想在关闭tar文件之前需要调用tar_append_eof http://linux.die.net/man/3/tar_append_eof.

tar_append_eof()函数将EOF标记(全部为两个块)写入与t关联的tar文件.