How To Create and Download Zip File in Laravel 10 Tutorial

Reading Time: 4 minutes
525 Views

You may need to bundle numerous files into a single, compressed ZIP as a Laravel developer for effective storage or dissemination.

This tutorial will walk you through the process of producing and downloading zip files in Laravel 10. Knowing how to make and deliver zip files is a crucial ability whether you’re constructing a file management system, producing backups, or facilitating file downloads for users.

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

URL: http://127.0.0.1:8000

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.

Next,

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.

<?php

namespace App\Http\Controllers;

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

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

        $fileName = 'my-images.zip';

        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);
            }

            $zip->close();
        }

        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

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/download-zip

When you hit this URL, it downloads my-images.zip 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.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more