php RarArchive & file_get_contents

php RarArchive & file_get_contents

我需要打开 rar 存档,搜索所有 *.txt 文件,读取这些文件并将内容放入 php 数组

$file="archive.rar";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$tmp=$_SERVER['DOCUMENT_ROOT'].'/tmp';

if($ext=='rar'){
    $archive = RarArchive::open($file);
    $entries = $archive->getEntries();
    foreach ($entries as $entry) {
        echo file_get_contents($entry->getName());// not working
        $entry->extract($tmp);
    }
    $archive->close();  
}

RarEntry::getName() method returns only path relative to archive. The file_get_contents() function knows nothing about this archive and it just try to find a regular file in current directory. You can use the rar:// 协议包装器根据格式:

rar://<url encoded archive name>[*][#[<url encoded entry name>]]

例如

echo file_get_contents('rar://' . $file . '#' . $entry->getName());