2014-09-19 32 views
0

我試圖使用libarchive庫重命名檔案的條目。 特別是我使用的功能archive_entry_set_pathname使用libarchive重命名檔案中的非空目錄

文件和空目錄正確改名,但不幸的是這是行不通的,如果一個目錄不爲空:不是被改名,新目錄用新的名稱作爲目標目錄的兄弟姐妹,創建了有舊的名字。

相關的代碼片段:

... 
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

在一個存檔中,這些文件將以其相對於存檔根的完整路徑名存儲。您的代碼只匹配目錄條目,您還需要匹配該目錄下的所有條目並重命名它們。我不是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 
    } 
}