I want to create a read stream that previously is gzencoded from a plain text.
The google cloud storage library has an upload function and you can pass a StreamInterface as parameter (Bucket::upload reference)
I want to upload a .txt file but gzencoded.
To upload a txt file is simple:
/** @var \Google\Cloud\Storage\Bucket $bucket */
$fd = fopen('/tmp/file.txt', 'r');
$stream = \GuzzleHttp\Psr7\Utils::streamFor($fd);
$bucket->upload($stream, ['name' => 'file.txt']);
I want to create a stream that:
- reads the original plain txt file
- does a gzencode in every chuck
And not storing the full file in memory (just the chunks) neither in disk. Is this possible?
I think it should be something like the following code, but creating a gz file (instead of zliz.deflating the data):
$fd = fopen('/tmp/file.txt', 'r');
stream_filter_append($fd, 'zlib.deflate', STREAM_FILTER_READ, ['window' => 15]);
$stream = Psr7\Utils::streamFor($fd);
$bucket->upload($stream, ['name' => 'file.txt.gz']);
Thanks!
I got a bit nerd-sniped and had to write something for this.
Reposting my above comment:
Well we can shim in a call to the system's gzip binary with
proc_open()
and stream the data through that to create a properly-formatted gzip stream.and we would use it like:
Which might output something like: