Laravel Create Zip File and Download Example
Hello Folks,
Today our leading topic is how to create zip file in laravel. This article will give you a simple example of laravel zip download response. I explained simply step by step laravel create zip file. I explained simply about laravel create zip file and download.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Use Enum in Laravel?
- How to Send Mail in Laravel 6?
- How to Add Flash Message in Laravel?
- Laravel 7/6 Resize Image Before Upload Example
- Laravel 6 Generate PDF File Tutorial
- Laravel 6 CRUD Application Tutorial
- Codeigniter Create Zip File and Download Example
- How to Create Zip Folder and Download in PHP?
- How to Create Zip File in Laravel?
- Laravel Export to PDF using Maatwebsite Example
- How to Count Files in a Directory using Laravel?
- Laravel Create JSON File & Download From Text Example