Laravel 10 Create and Download Zip File Code Example

18-Apr-2023

.

Admin

Laravel 10 Create and Download Zip File Code Example

Hey Developer,

The response to laravel 10 zip download is the main topic of this tutorial. This tutorial covers how to produce a zip file in Laravel 10 in depth. You've come to the right place if you want to see an example of how to produce a zip file and download it using Laravel 10. Laravel 10 create zip from folder, as seen.

Large volumes of data are frequently organised and compressed using the zip file format. It is a well-liked technique for lowering the size of huge files or folders for better sharing and storage. Zip files reduce the size of several files or folders while preserving their structure and contents by compressing them into a single archive. As a result, they are simpler to download or share online and take up less space when being transferred or stored.

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...

#Laravel 10