我在
.tar.gz
存档中有一个特定文件,我想提取该文件。我如何在 PHP 中执行此操作?
例如,MaxMind 的 GeoLite 数据库 可作为
.tar.gz
文件使用。对于每个产品,文件名是 GeoLite2-ProductName.tar.gz
,里面有一个 GeoLite2-ProductName_yyymmdd
目录,里面有一个 GeoLite2-ProductName.mmdb
文件以及一些其他文本文件。我只想提取.mmdb file
。
使用 PHP phar,打开
.tar.gz
存档,迭代文件,然后仅提取您感兴趣的文件名。
$phar = new \PharData('/var/www/html/GeoLite2-ProductName.tar.gz');
foreach (new \RecursiveIteratorIterator($phar) as $file) {
if (str_ends_with($file, 'GeoLite2-ProductName.mmdb')) {
$contents = file_get_contents($file->getPathName());
file_put_contents('/var/www/html/GeoLite2-ProductName.mmdb', $contents);
break;
}
}