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 the laravel 8 database seeder example. As we all know laravel framework provides...
Oct-23-2020
In this guide, we'll see how to send email using a queue in laravel 11. Here we'll see the concept of queue...
Apr-12-2024
Hello, laravel web developers! In this article, we'll see how to generate fake data using Tinker in laravel 11....
May-22-2024
In this tutorial, I will explain the laravel 9 image upload example. image or file upload is the most common task in web...
Feb-28-2022