使用libarchive重命名存档中的非空目录

时间:2014-09-19 13:06:32

标签: c++ libarchive

我正在尝试使用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
        }
    }

非空目录有什么问题?

1 个答案:

答案 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
    }
}