Stream a file chunk by chunk #178
-
Hello, I am currently using I use the For the options configuration : $options = new Archive();
$options->setContentType('application/zip');
$options->setContentDisposition('attachment; filename="test.zip"');
$options->setZeroHeader(true);
$options->setComment('test zip file.');
$options->setSendHttpHeaders(true); For now, I'm going through temporary in-memory storage : $stream = fopen('php://memory', 'ab+'); The chunk being encrypted, I decrypt it and transform the returned character string into a resource to be able to pass it to the function. $streamRead = \openssl_decrypt(
stream_get_contents(fopen($s3path, 'rb')),
/* ... */
);
fwrite($stream, $streamRead);
rewind($stream);
$zip->addFileFromStream($fileName, $stream); Unfortunately, by doing this each chunk overwrites the previous one instead of ending at the end of the file. Thanks for your help ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
It should probably be possible to implement a custom class implementing PSR7 StreamInterface to do this. EDIT: This could work something like that (completely untested pseudo code): use Psr7\AppendStream;
use Psr7\Utils;
$zip = new ZipStream('test.zip', $options);
foreach ($s3keys as $key => $value) {
$fileName = $value;
// $chunks = get chunks from S3;
$fileStream = new AppendStream(array_map(function($chunk) using($bucket, $key, $value) {
$path = sprintf('s3://%s/%s/%s/%s', $bucket, $key, $chunk->getChunk(), basename($value));
return Utils::streamFor(openssl_decrypt(fopen($path, 'rb'), /* ... */));
}, $chunks));
$zip->addFileFromPsr7Stream($fileName, $fileStream);
}
$zip->finish(); |
Beta Was this translation helpful? Give feedback.
It should probably be possible to implement a custom class implementing PSR7 StreamInterface to do this.
EDIT:
This could work something like that (completely untested pseudo code):