在我的程序中,我需要从.tar文件中读取.png文件。
我使用的是pear Archive_Tar类(http://pear.php.net/package/Archive_Tar/redirected)
如果im查找的文件存在,则一切正常,但是如果它不在.tar文件中,则函数在30秒后超时。在类文档中,它声明如果找不到文件,则应返回null ...
$tar = new Archive_Tar('path/to/mytar.tar');
$filePath = 'path/to/my/image/image.png';
$file = $tar->extractInString($filePath); // This works fine if the $filePath is correct
// if the path to the file does not exists
// the script will timeout after 30 seconds
var_dump($file);
return;有什么建议可以解决这个问题或任何其他我可以用来解决问题的库吗?
发布于 2014-06-19 20:50:33
listContent方法将返回指定存档中存在的所有文件(以及有关这些文件的其他信息)的数组。因此,如果您首先检查要提取的文件是否存在于该数组中,则可以避免遇到的延迟。
下面的代码没有经过优化-对于提取不同文件的多次调用,例如$files数组应该只填充一次-但这是一种很好的前进方式。
include "Archive/Tar.php";
$tar = new Archive_Tar('mytar.tar');
$filePath = 'path/to/my/image/image.png';
$contents = $tar->listContent();
$files = array();
foreach ($contents as $entry) {
$files[] = $entry['filename'];
}
$exists = in_array($filePath, $files);
if ($exists) {
$fileContent = $tar->extractInString($filePath);
var_dump($fileContent);
} else {
echo "File $filePath does not exist in archive.\n";
}https://stackoverflow.com/questions/24302922
复制相似问题