How to Create ZIP Archive File in Laravel 11?
In this post, I will show you how to create and download zip file in laravel 11 application.
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 simply create a folder myFiles and create a 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 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 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
- Laravel 11 Google Autocomplete Address Example
- Laravel 11 Ajax Dependent Dropdown Example
- How to Save JSON Data in Database in Laravel 11?
- Setup Automatic Daily Database Backup with Laravel 11
- Laravel 11 CRUD with Image Upload Tutorial
- Laravel 11 Razorpay Payment Gateway Integration Example
- How to Install and Configuration Telescope in Laravel 11?
- Laravel 11 Google Recaptcha V3 Validation Tutorial
- Laravel 11 Image Intervention Tutorial With Example
- Laravel 11 Summernote Image Upload Tutorial
- Laravel 11 Dynamic Google Charts Integration Tutorial
- Laravel 11 Socialite Login with Twitter / X Account Example
- Laravel 11 Cron Job Task Scheduling Tutorial
- Laravel 11 CRUD Application Example Tutorial