使用php获取tar.gz文件中的文件总数


Get total number of files inside a tar.gz file using php

我是php新手,但在一个项目中,我想计算有tar.gz文件的数量,我知道如何计算zip文件,但我的问题是:我可以(用php函数)计算有多少文件在tar.gz文件?

使用php,我可以计算zip文件中的文件数量,代码如下

$zip = new ZipArchive(); 
if($zip->open("/path/to/the/zip")===true)
{
    print $zip->numFiles;
}
else
{
    print "cant open zip file";
}

但是对于tgz文件,我可以做一些类似的(不使用exec函数)吗?

谢谢:)


感谢kinsey,我有一个答案(对他的代码进行了一些修改)。最后的代码是

<?php
$archive = new PharData('/route/to/file.tar.gz');
$i=0;
foreach($archive as $file) 
{
    $i+=1;
}
print $i;
?>

谢谢@Kkinsey

Try PharData:

// Example: list files
$archive = new PharData('/some/file.tar.gz');
foreach($archive as $file) {
        echo "$file'n";
}