Laravel 11 Create ZIP Archive File Example

09-May-2024

.

Admin

Laravel 11 Create ZIP Archive File Example

Hi, Dev

In this tutorial, I'll guide you through the process of generating and downloading a zip file within your Laravel 11 application.

A zip file serves as a compressed archive format widely employed for organizing and condensing substantial volumes of data. It stands as a favored method for minimizing the size of large files or directories, facilitating more efficient storage and distribution. Functioning through the compression of one or more files or directories into a unified archive, zip files reduce their dimensions while preserving their inherent structure and contents. Consequently, they become simpler to transfer or store, occupying lesser space and enabling easy downloading or sharing across the internet.

In this guide, I'll demonstrate two methods for generating a zip file from a folder in Laravel. The first utilizes ZipArchive, while the second leverages the stechstudio/laravel-zipstream package. We'll start by creating a folder named "myFiles" and subsequently craft a zip file from its contents. Let's delve into both examples sequentially.

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 4: 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

I hope it can help you...

#Laravel 11