Created
March 3, 2021 12:13
-
-
Save BR0kEN-/1fc11cc8449afade6d01858e41a1b124 to your computer and use it in GitHub Desktop.
Guzzle unarchive Gzip on the fly (without storing an archive on disk)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Requirements: | |
// - ext-zlib | |
// - guzzlehttp/guzzle | |
declare(strict_types=1); | |
use GuzzleHttp\Client; | |
use GuzzleHttp\RequestOptions; | |
use Psr\Http\Message\StreamInterface; | |
require 'vendor/autoload.php'; | |
function gzip_inflate(string $file_path, StreamInterface $stream, int $chunk_size): \SplFileObject { | |
\touch($file_path); | |
$file_object = new \SplFileObject($file_path, 'wb'); | |
$zlib_resource = \inflate_init(\ZLIB_ENCODING_GZIP); | |
while (!$stream->eof()) { | |
$file_object->fwrite(\inflate_add($zlib_resource, $stream->read($chunk_size))); | |
} | |
$file_object->fflush(); | |
return $file_object; | |
} | |
$response = (new Client())->get('https://tgwf-green-domains-live.s3.nl-ams.scw.cloud/green_urls_2021-02-19.db.gz', [ | |
RequestOptions::STREAM => true, | |
RequestOptions::TIMEOUT => 120, | |
]); | |
if (200 === $status_code = $response->getStatusCode()) { | |
// Inflate a megabyte per chunk. | |
gzip_inflate('./database.db', $response->getBody(), 1024 ** 2); | |
} else { | |
echo \sprintf('Download failed (HTTP %d).', $status_code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment