The code in the first exemple doesn't work properly without the flag FilesystemIterator::SKIP_DOTS in the RecursiveDirectoryIterator (on a linux filesystem).
So, to build a tar with a phar, I do (with two level of iteration here) :
$pharTar = new \PharData($contentTar.".tar");
$firstLevelIterator = new \DirectoryIterator($this->inputPath);
foreach ($firstLevelIterator as $fileInfo) {
/* @var \SplFileInfo $fileInfo */
if (in_array($fileInfo->getFilename(), $allowedDirectory)) {
$recursiveDirectoryIterator = new \RecursiveDirectoryIterator(
$this->inputPath . DIRECTORY_SEPARATOR . $fileInfo->getFilename(), \FilesystemIterator::SKIP_DOTS);
$pharTar->buildFromIterator(new \RecursiveIteratorIterator($recursiveDirectoryIterator), $this->inputPath);
}
}