Skip to content

Instantly share code, notes, and snippets.

@BR0kEN-
Created March 3, 2021 12:13
Show Gist options
  • Save BR0kEN-/1fc11cc8449afade6d01858e41a1b124 to your computer and use it in GitHub Desktop.
Save BR0kEN-/1fc11cc8449afade6d01858e41a1b124 to your computer and use it in GitHub Desktop.
Guzzle unarchive Gzip on the fly (without storing an archive on disk)
<?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