我正在尝试使用libarchive
库重命名存档的条目。
特别是我正在使用函数archive_entry_set_pathname
。
文件和空目录已正确重命名,但遗憾的是,如果目录不为空,则无效:不是重命名,而是创建一个带有新名称的新空目录作为兄弟的目录。目标目录,具有旧名称。
相关代码段:
...
while (archive_read_next_header(inputArchive, &entry) == ARCHIVE_OK) {
if (file == QFile::decodeName(archive_entry_pathname(entry))) {
// FIXME: not working with non-empty directories
archive_entry_set_pathname(entry, QFile::encodeName(newPath));
}
int header_response;
if ((header_response = archive_write_header(outputArchive, entry)) == ARCHIVE_OK) {
... // write the (new) outputArchive on disk
}
}
非空目录有什么问题?
答案 0 :(得分:1)
在存档中,文件以其相对于存档根目录的完整路径名存储。您的代码仅匹配目录条目,您还需要匹配该目录下的所有条目并重命名它们。我不是Qt专家,我没有尝试过这段代码,但你会明白这一点。
QStringLiteral oldPath("foo/");
QStringLiteral newPath("bar/");
while (archive_read_next_header(inputArchive, &entry) == ARCHIVE_OK) {
QString arEntryPath = QFile::decodeName(archive_entry_pathname(entry));
if(arEntryPath.startsWith(oldPath) {
arEntryPath.replace(0, oldPath.length(), newPath);
archive_entry_set_pathname(entry, QFile::encodeName(arEntryPath));
}
int header_response;
if ((header_response = archive_write_header(outputArchive, entry)) == ARCHIVE_OK) {
... // write the (new) outputArchive on disk
}
}