How to Create Zip File and Download in Laravel 10?

Example 1: 

Step 1: Install Laravel


    composer create-project laravel/laravel example-app

Step 2: Create Route

routes/web.php 


<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ZipController;
   
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
    
Route::get('download-zip', ZipController::class);

Step 3: Create Controller

Same things as above for route, here we will add one new method for route. __invoke() will generate new zip file and download as response, so let's add bellow:

app/Http/Controllers/ZipController.php


<?php
  
    namespace App\Http\Controllers;
      
    use Illuminate\Http\Request;
    use File;
    use ZipArchive;
      
    class ZipController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function __invoke()
        {
            $zip = new ZipArchive;
        
            $fileName = 'myNewFile.zip';
         
            if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
            {
                $files = File::files(public_path('myFiles'));
         
                foreach ($files as $key => $value) {
                    $relativeNameInZipFile = basename($value);
                    $zip->addFile($value, $relativeNameInZipFile);
                }
                   
                $zip->close();
            }
          
            return response()->download(public_path($fileName));
        }
    }

Run Laravel App:

php artisan serve

Now you can open bellow URL on your browser:


    http://localhost:8000/download-zip

Example 2:

Step 1: Install Laravel 


   composer create-project laravel/laravel example-app

Step 2: Install stechstudio/laravel-zipstream Package

routes/web.php 


composer require stechstudio/laravel-zipstream

Step 3: Create Route


<?php
  
    use Illuminate\Support\Facades\Route;
      
    use App\Http\Controllers\ZipController;
       
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
        
    Route::get('download-zip', ZipController::class);

Step 4: Create Controller

app/Http/Controllers/ZipController.php 


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use Zip;
class ZipController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __invoke()
    {
        return Zip::create('zipFileName.zip', File::files(public_path('myFiles')));
    }
}

Run Laravel App:


php artisan serve