How to Create Zip File and Download in Laravel 10?

By Hardik Savani April 20, 2024 Category : Laravel

Hey Developer,

This tutorial is focused on laravel 10 zip download response. This article goes in detailed on laravel 10 create zip file. if you want to see an example of laravel 10 create zip file and download then you are in the right place. you can see laravel 10 create zip from folder.

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 simple create folder myFiles and create zip file from that folder. so, let's see both examples one by one.

Example 1:

Let's see this example using ZipArchive class.

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

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;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

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;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

I hope it can help you...

Shares