使用libarchive重命名存档中的非空目录
Rename a non-empty directory in an archive using 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
}
}
非空目录有什么问题?
在归档中,文件存储时使用相对于归档根的完整路径名。您的代码只匹配目录条目,还需要匹配该目录下的所有条目并重命名它们。我不是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
}
}
相关文章:
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 通过重命名C++中的符号来减小二进制大小
- 如何以与 API 兼容的方式重命名类成员?
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 在Qt中复制,剪切,粘贴,重命名,撤消和重做
- 重命名 win32api 函数以进行混淆
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 如何在重命名函数 (c++) 中使用变量?
- 如何通过 boost::p ython 重命名构造函数的关键字参数
- 以编程方式防止重命名或删除文件,但仍使其可写
- C++:重命名dll和lib并链接
- 使用 stdio.h 重新定位和重命名文件C++
- C++ Qt5 QDir重命名返回错误
- C++提升 ptree 重命名键
- 如何检测哪些进程更改,重命名或创建文件?
- 禁用/重命名从主机环境导入"env"
- 录制后无法重命名 rosbag 文件
- 避免在统计数据和重命名之间进行TOCTOU(检查时间,使用时间)
- 将文件重命名为已存在的文件名
- 使用libarchive重命名存档中的非空目录