How to Create ZIP Archive File in Laravel 11?
In this tutorial, we’ll learn how to create and download a ZIP file in a Laravel 11 application. ZIP files are commonly used for organizing and compressing large amounts of data, making them easier to store and share.
This guide will cover two methods: using the ZipArchive
class and using the stechstudio/laravel-zipstream
package.
We will create a folder named myFiles
and generate a ZIP file from that folder. Let’s dive into the details of both methods.
Method 1 : Using ZipArchive Class
Step 1 : Install Laravel 11
First, let’s install a fresh Laravel 11 application. Open your terminal and run the following command :
composer create-project laravel/laravel example-app
Step 2 : Create a Route
Next, we need to create a route for generating and downloading the ZIP file. Open the routes/web.php
file and add the following route :
Step 3 : Create a Controller
Now, let’s create a controller to handle the ZIP file creation. Use the artisan command to create the controller :
php artisan make:controller ZipController
Update the ZipController
with the following code:
open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = File::files(public_path('myFiles'));
foreach ($files as $file) {
$relativeNameInZipFile = basename($file);
$zip->addFile($file, $relativeNameInZipFile);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}
Step 4 : Prepare Files for Zipping
Make sure you have a myFiles
folder in the public
directory and add some files to this folder. These files will be included in the ZIP archive.
Step 5 : Run the Laravel Application
Run your Laravel application using the following command :
php artisan serve
Open your browser and navigate to http://localhost:8000/download-zip
. This URL will trigger the ZIP file creation and download.
Method 2 : Using stechstudio/laravel-zipstream Package
Step 1 : Install Laravel 11
First, install a fresh Laravel 11 application if you haven’t already :
composer create-project laravel/laravel example-app
Step 2 : Install ZipStream Package
Next, install the stechstudio/laravel-zipstream
package using Composer :
composer require stechstudio/laravel-zipstream
Step 3 : Create a Route
Add a route for generating and downloading the ZIP file in routes/web.php
:
Step 4 : Create a Controller
Create the ZipController
using the artisan command and update it with following code :
php artisan make:controller ZipController
Step 5 : Prepare Files for Zipping
Ensure you have a myFiles
folder in the public
directory and add some files to this folder.
Step 6 : Run the Laravel Application
Run your Laravel application:
php artisan serve
Open your browser and navigate to http://localhost:8000/download-zip
. This URL will trigger the ZIP file creation and download.
Conclusion :
In this tutorial, we covered two methods for creating and downloading ZIP files in a Laravel 11 application: using the ZipArchive
class and the stechstudio/laravel-zipstream
package.
Both methods allow you to compress and organize files into a single archive for easier storage and sharing. Choose the method that best suits your needs and integrate it into your Laravel projects.
Now, let’s create a controller to handle the ZIP file creation. Use the artisan command to create the controller :