Как добавить пустые папки и символические ссылки в архив - libarchive - PullRequest
0 голосов
/ 25 августа 2018

Я пытаюсь сжать папку в архив 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. Распаковка файлов с использованием libarchive работает нормально. Он извлек все файлы, папки и символические ссылки ....

полный код для сжатия здесь

1 Ответ

0 голосов
/ 06 сентября 2018

В любом случае исправил это 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

...