How to Create ZIP Archive File in Laravel 11?

By Hardik Savani October 19, 2024 Category : Laravel

In this post, I will show you how to create and download zip file in laravel 11 application.

A zip file is a compressed archive format commonly used for organizing and compressing large amounts of data. It is a popular way of reducing the size of large files or folders for more efficient storage and sharing. Zip files work by compressing one or more files or folders into a single archive, reducing their size while maintaining their structure and contents. This makes them easier to transfer or store, as they take up less space and can be easily downloaded or shared over the internet.

In this post, I will show you the following two ways to generate a zip file from the folder in Laravel. One using ZipArchive and another using stechstudio/laravel-zipstream package. We will simply create a folder myFiles and create a zip file from that folder. So, let's see both examples one by one.

laravel 11 create zip file

Example 1:

Let's see this example using ZipArchive class.

Step 1: Install Laravel 11

First of all, we need to get a fresh Laravel 11 version application using the command below because we are starting from scratch. So, open your terminal or command prompt and run the command below:

composer create-project laravel/laravel example-app

Step 2: Create Route

In this is step we need to create route for create and download zip file. so open your "routes/web.php" file and add following route.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ZipController;
    
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));
    }
}

Ok now you can run project and open that route like.

But make sure you have "myFiles" folder in public directory and add some pdf files on that file so it will create zip file with those files.

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/download-zip

Example 2:

Let's see this example using stechstudio/laravel-zipstream composer package.

Step 1: Install Laravel 11

First of all, we need to get a fresh Laravel 11 version application using the command below because we are starting from scratch. So, open your terminal or command prompt and run the command below:

composer create-project laravel/laravel example-app

Step 2: Install stechstudio/laravel-zipstream Package

In this step, we will install stechstudio/laravel-zipstream composer package using the following command:

composer require stechstudio/laravel-zipstream

Step 3: Create Route

In this is step we need to create route for create and download zip file. so open your "routes/web.php" file and add following route.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ZipController;
    
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 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')));
    }
}

Ok now you can run project and open that route like.

But make sure you have "myFiles" folder in public directory and add some pdf files on that file so it will create zip file with those files.

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/download-zip

Output:

I hope it can help you...

Shares