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
Laravel 8 Database Seeder Example
Laravel 8 Database Seeder Exam...

In this article, we will see the laravel 8 database seeder example. As we all know laravel framework provides...

Read More

Oct-23-2020

Mail: Laravel 11 Send Email using Queue
Mail: Laravel 11 Send Email us...

In this guide, we'll see how to send email using a queue in laravel 11. Here we'll see the concept of queue...

Read More

Apr-12-2024

How to Generate Fake Data using Tinker in Laravel 11
How to Generate Fake Data usin...

Hello, laravel web developers! In this article, we'll see how to generate fake data using Tinker in laravel 11....

Read More

May-22-2024

Laravel 9 Image Upload Example
Laravel 9 Image Upload Example

In this tutorial, I will explain the laravel 9 image upload example. image or file upload is the most common task in web...

Read More

Feb-28-2022