When downloading the files try using streams rather than trying to store the content of the file in a variable, then writing that to a file. This will prevent you from having to load the entire file into memory which is likely where you are having issues.
// Download file
$source = Storage::disk('s3')->readStream('path/to/file.txt');
$tmpfname = tempnam(sys_get_temp_dir(), "tmp");
$destination = fopen($tmpfname, "w");
while (!feof($source)) {
fwrite($destination, fread($source,8192));
}
fclose($source);
fclose($destination);
// Add file to zip
$zipfile = sys_get_temp_dir() . '/file.zip';
$zip = new ZipArchive;
$zip->open($zipfile);
$zip->addFile($tmpfname, 'file.txt');
$zip->close();
unlink($tmpfname);
// upload zip back to S3, or you could stream it to the browser if that is what you need. Make sure to delete the local file after though
// To upload back to S3
Storage::disk('s3')->put('file.zip', $zipfile);
unlink($zipfile);
// to stream the file to the browser
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
$response = new StreamedResponse();
$response->setCallback(function () use($zipfile){
readfile($zipfile);
unlink($zipfile);
});
$response->headers->set('Content-Type', 'application/x-zip-compressed');
$response->headers->set('Cache-Control', '');
$response->headers->set('Content-Length', filesize($zipfile));
$response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s'));
$contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, basename($zipfile));
$response->headers->set('Content-Disposition', $contentDisposition);
return $response;