diff --git a/README.md b/README.md index e4c57c8..8620cb9 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,13 @@ You can update several databases: php bin/console geoip2:update city country ``` +Optionally installing splitbrain/php-archive uses significantly less memory when updating a database +and can avoid out of memory errors: + +``` +composer req splitbrain/php-archive +``` + ### Download GeoIP database You can download custom database with console command: diff --git a/composer.json b/composer.json index a14ee9e..54ed0fb 100644 --- a/composer.json +++ b/composer.json @@ -30,5 +30,8 @@ "phpunit/phpunit": "~7.0|~8.0|~9.0", "phpstan/phpstan": "^0.12", "phpstan/phpstan-phpunit": "^0.12" + }, + "suggest": { + "splitbrain/php-archive": "Greatly reduces memory usage for the geoip2:update command" } } diff --git a/src/Downloader/MaxMindDownloader.php b/src/Downloader/MaxMindDownloader.php index 3bc80de..18adfa3 100644 --- a/src/Downloader/MaxMindDownloader.php +++ b/src/Downloader/MaxMindDownloader.php @@ -12,6 +12,7 @@ namespace GpsLab\Bundle\GeoIP2Bundle\Downloader; use Psr\Log\LoggerInterface; +use splitbrain\PHPArchive\Tar; use Symfony\Component\Filesystem\Filesystem; /** @@ -67,20 +68,32 @@ public function download(string $url, string $target): void $this->fs->copy($url, $tmp_zip, true); $this->logger->debug(sprintf('Download complete to %s', $tmp_zip)); - $this->logger->debug(sprintf('De-compressing file to %s', $tmp_unzip)); $this->fs->mkdir(dirname($target), 0755); - // decompress gz file - $zip = new \PharData($tmp_zip); - $tar = $zip->decompress(); + if (class_exists(Tar::class)) { + $this->logger->debug(sprintf('Extracting archive file to %s', $tmp_untar)); - $this->logger->debug('Decompression complete'); - $this->logger->debug(sprintf('Extract tar file to %s', $tmp_untar)); + // extract tar.gz archive + $tar = new Tar(); + $tar->open($tmp_zip); + $tar->extract($tmp_untar); + $tar->close(); + unset($tar); + } else { + $this->logger->debug(sprintf('De-compressing file to %s', $tmp_unzip)); - // extract tar archive - $tar->extractTo($tmp_untar); - unset($zip, $tar); + // decompress gz file + $zip = new \PharData($tmp_zip); + $tar = $zip->decompress(); + + $this->logger->debug('Decompression complete'); + $this->logger->debug(sprintf('Extract tar file to %s', $tmp_untar)); + + // extract tar archive + $tar->extractTo($tmp_untar); + unset($zip, $tar); + } $this->logger->debug('Tar archive extracted');