C++ 如何添加空文件夹&;指向存档的符号链接-libarchive

C++ 如何添加空文件夹&;指向存档的符号链接-libarchive,c++,libarchive,C++,Libarchive,我正试图用以下代码将一个文件夹压缩到cpio.gz归档文件中。但它不会压缩空文件夹和符号链接 void write_archive(string archivename, vector<string> files) { struct archive *a; struct archive_entry *entry; struct stat st; char buff[8192]; int len; int fd; a = arc

我正试图用以下代码将一个文件夹压缩到cpio.gz归档文件中。但它不会压缩空文件夹和符号链接

void write_archive(string archivename, vector<string> files) {
    struct archive *a;
    struct archive_entry *entry;
    struct stat st;
    char buff[8192];
    int len;
    int fd;

    a = archive_write_new();
    archive_write_add_filter_gzip(a);
    archive_write_set_format_cpio(a);
    archive_write_open_filename(a, archivename.c_str());
    for (string file : files) {
        string filename = file;
        stat(file.c_str(), &st);
        entry = archive_entry_new();
        archive_entry_set_pathname(entry, trim(filename));
        archive_entry_set_size(entry, st.st_size);
        archive_entry_set_filetype(entry, AE_IFREG);
        archive_entry_set_perm(entry, 0644);
        archive_write_header(a, entry);
        fd = open(file.c_str(), O_RDONLY);
        len = read(fd, buff, sizeof(buff));
        while ( len > 0 ) {
            archive_write_data(a, buff, len);
            len = read(fd, buff, sizeof(buff));
        }
        close(fd);
        archive_entry_free(entry);
    }
    archive_write_close(a);
    archive_write_free(a);
}
void write_归档(字符串归档名称、矢量文件){
结构档案*a;
结构存档项目*项目;
结构统计;
字符buff[8192];
内伦;
int-fd;
a=存档\写入\新建();
归档、写入、添加、过滤(a);
存档、写入、设置、格式、cpio(a);
归档文件名(a,archivename.c_str());
用于(字符串文件:文件){
字符串文件名=文件;
stat(file.c_str(),&st);
entry=archive_entry_new();
归档\条目\集合\路径名(条目、修剪(文件名));
归档项目集大小(项目、标准项目大小);
归档\u条目\u集合\u文件类型(条目,AE\u IFREG);
归档、条目、集合、perm(条目,0644);
存档\写入\头(a,条目);
fd=open(file.c_str(),O_RDONLY);
len=读取(fd,buff,sizeof(buff));
而(len>0){
存档写入数据(a、buff、len);
len=读取(fd,buff,sizeof(buff));
}
关闭(fd);
存档项目免费(项目);
}
存档、写入、关闭(a);
免费存档(a);
}
我正在使用这段代码重新打包Android提取的ramdisk。使用libarchive提取文件工作正常。它提取了所有文件、文件夹和符号链接

压缩的完整代码已修复

关键是

archive_entry_set_filetype(entry, AE_IFLNK); - to set the type of entity which we can determine with help of lstat

参考资料:

archive_entry_set_symlink(entry, link.c_str()); // - to set the path of link