How To Create Zip File And Download In laravel 9

In this article, we will see how to create a zip file and download it in laravel 9. Laravel provides ZipArchive class for creating zip files in laravel 9. I will give you examples step by step of laravel 9 create a zip file from a folder and download it. we will create a zip file using the zip archive class in PHP and laravel 9 application.

So, let's see create a zip file in laravel 9, and create a zip file using ziparchive in laravel 9.

Step 1: Create Route

In this step, we will create a route in the web.php file


use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ZipArchiveController;
| 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', [ZipArchiveController::class, 'downloadZip']);



Step 2: Create Controller

Now, create a new controller name as ZipArchiveController.



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ZipArchive;

class ZipArchiveController extends Controller
    public function downloadZip()
        $zip = new ZipArchive;

        $fileName = '';

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
            $files = \File::files(public_path('Zip_Example'));

            foreach ($files as $key => $value) {
                $file = basename($value);
                $zip->addFile($value, $file);

        return response()->download(public_path($fileName));

Note: I have created a Zip_Example folder in the public folder and added some images. So, you need to also create one folder and add some files also.




You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.