我正在尝试使用以下代码将文件夹压缩到 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 提取文件工作正常。它提取了所有文件,文件夹和符号链接。
此处压缩的完整代码
无论如何修复了它 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