Hello, laravel web developers! In this article, we'll see how to create a zip archive file in laravel 11. You can create a zip file using two methods. one is using ZipArchive and the other is using the stechstudio/laravel-zipstream composer package.
stechstudio/laravel-zipstream is an easy to create zip file and provides a streaming download. Also, you build zip files from local or S3 file sources.
Laravel 11 Create Zip File Example
Example 1: Using ZipArchive
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel example-app
Next, we'll define the routes into the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;
Route::get('zip-file', ZipController::class);
Then, we'll create a controller and create a zip file. Also, we'll use the ZipArchive PHP library.
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 = 'test.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = File::files(public_path('test'));
foreach ($files as $key => $value) {
$relativeNameInZipFile = basename($value);
$zip->addFile($value, $relativeNameInZipFile);
}
$zip->close();
}
return response()->download(public_path($fileName));
}
}
Note: Create a test folder in the public directory and put some files into that folder.
Example 2: Using stechstudio/laravel-zipstream
In this step, we'll install stechstudio/laravel-zipstream composer package using the following command.
composer require stechstudio/laravel-zipstream
Now, we'll define the routes into the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;
Route::get('zip-file', ZipController::class);
Then, we'll create a controller and create a zip file. Also, we'll use the ZipArchive PHP library.
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('test.zip', File::files(public_path('test')));
}
}
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like:
In this article, we will see how to create a pie chart in laravel 9 using highcharts. A pie chart is a circular statisti...
Oct-05-2022
In this article, we will see laravel 9 livewire toastr notification. Here, we will learn how to create toastr notif...
Nov-28-2022
Today we will learn next previous link button pagination in laravel, Using paginate method you can easily create paginat...
Jun-14-2021
In this article, we'll install the material theme in angular 17. Material Theme brings a polished design and us...
Mar-29-2024