@cali_green
To gzip a folder in a Laravel project, you can use the ZipArchive
class provided by PHP. Here's an example of how you can achieve this:
1
|
php artisan make:controller GzipController |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
use ZipArchive; use IlluminateHttpRequest; public function gzipFolder(Request $request) { $dir = public_path('folder-to-gzip'); // specify the folder to be gzipped $zipFile = public_path('compressed-folder.zip'); // path of the output gzipped file $zip = new ZipArchive; if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); foreach ($files as $file) { $filePath = $file->getRealPath(); $relativePath = substr($filePath, strlen($dir) + 1); if (is_file($filePath)) { $zip->addFile($filePath, $relativePath); } } $zip->close(); return response()->download($zipFile)->deleteFileAfterSend(true); } else { return response()->json(['message' => 'Failed to create compressed file'], 500); } } |
1
|
Route::get('/gzip-folder', 'GzipController@gzipFolder'); |
Please make sure to replace folder-to-gzip
with the path of the folder you want to gzip and compressed-folder.zip
with the desired output gzipped file name.