How to Create ZIP Archive File in Laravel 11 Example

Websolutionstuff | Jul-17-2024 | Categories : Laravel

Hello, laravel web developers! In this article, we'll see how to create a zip archive file in laravel 11. You can create a zip file using two methods. one is using ZipArchive and the other is using the stechstudio/laravel-zipstream composer package.

stechstudio/laravel-zipstream is an easy to create zip file and provides a streaming download. Also, you build zip files from local or S3 file sources.

Laravel 11 Create Zip File Example

laravel 11 create zip file

 

Example 1: Using ZipArchive

Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel example-app

 

Step 2: Define Route

Next, we'll define the routes into the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ZipController;
    
Route::get('zip-file', ZipController::class);

 

Step 3: Create Controller

Then, we'll create a controller and create a zip file. Also, we'll use the ZipArchive PHP library.

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 = 'test.zip';
     
        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
            $files = File::files(public_path('test'));
     
            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
            }
               
            $zip->close();
        }
      
        return response()->download(public_path($fileName));
    }
}

Note: Create a test folder in the public directory and put some files into that folder.

 

Example 2: Using stechstudio/laravel-zipstream

Step 1: Install stechstudio/laravel-zipstream Package

In this step, we'll install stechstudio/laravel-zipstream composer package using the following command.

composer require stechstudio/laravel-zipstream

 

Step 2: Define Route

Now, we'll define the routes into the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ZipController;
    
Route::get('zip-file', ZipController::class);

 
Step 3: Create Controller

Then, we'll create a controller and create a zip file. Also, we'll use the ZipArchive PHP library.

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('test.zip', File::files(public_path('test')));
    }
}

 

Step 4: Run the Laravel Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How To Create Pie Chart In Laravel 9 Using Highcharts
How To Create Pie Chart In Lar...

In this article, we will see how to create a pie chart in laravel 9 using highcharts. A pie chart is a circular statisti...

Read More

Oct-05-2022

Laravel 9 Livewire Toastr Notification
Laravel 9 Livewire Toastr Noti...

In this article, we will see laravel 9 livewire toastr notification. Here, we will learn how to create toastr notif...

Read More

Nov-28-2022

Next Previous Link Button Pagination in Laravel
Next Previous Link Button Pagi...

Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create paginat...

Read More

Jun-14-2021

How to Install Material Theme in Angular 17
How to Install Material Theme...

In this article, we'll install the material theme in angular 17. Material Theme brings a polished design and us...

Read More

Mar-29-2024