如何将空文件夹和符号链接添加到存档-libarchive

时间:2018-08-25 09:23:52

标签: 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 = 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);
}

我正在使用此代码重新打包提取的Android ramdisk。使用libarchive提取文件工作正常。它提取了所有文件,文件夹和符号链接。...

用于压缩here

的完整代码

1 个答案:

答案 0 :(得分:0)

无论如何都修复了https://github.com/Devil7DK/SimpleShits/commit/2859df5855ae26c63240a0ea99bf5f015d810b66

关键是

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
  

参考文献:

     

https://github.com/libarchive/libarchive/issues/1034   https://www.unix.com/man-page/freebsd/3/archive_entry_set_symlink/   https://groups.google.com/forum/#!topic/libarchive-discuss/iz5wCAW2GZ4