How To Create and Download Zip File in Laravel 10 Tutorial

Share this Article
Reading Time: 3 minutes

Inside this article we will see the concept How To Create and Download Zip File in Laravel 10 Tutorial. Article contains classified information about Step-by-Step Guide to Creating and Downloading Zip Files in Laravel 10.

We will use the concept of File and ZipArchive. ZipArchive is a Laravel package that makes it simple and quick to create and manage zip archives. Developers can use it to add files and directories to zip archives, extract files from zip archives, and compress or decompress zip archives.

To use the ZipArchive package in Laravel, developers need to first install it using Composer.

Read More: How To Query To Get Single Row Data in Laravel 10 Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve


Assuming laravel already installed inside your system.

Collection of Files to Create Zip

Suppose we have a folder with name images in /public folder and we want to create a zip file of it and download via browser.

Inside images folder we have some images.


We need to write logic to create zip file of images folder.

Create Zip Controller

Open project into terminal and run this artisan command to create controller file.

$ php artisan make:controller ZipController

It will create ZipController.php inside /app/Http/Controllers folder.

Read More: Form Validation with Ajax Request in Laravel 10 Tutorial

This controller contains all the logic to create zip file and download that.

Open ZipController.php and write this complete code into it.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
use ZipArchive;

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

        $fileName = '';

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

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


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

Add Routes

Open web.php from /routes folder and add this route into it.


use App\Http\Controllers\ZipController;

Route::get('download-zip', [ZipController::class, "downloadZip"]);


Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve


When you hit this URL, it downloads file into your system

Read More: How To Get File Size From File Path in Laravel 10 Tutorial

We hope this article helped you to learn How To Create and Download Zip File in Laravel 10 Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.