Let's get the Laravel 9 zip files in this post when we've zipped the files. This guide will show you how to use the ZipArchive class and the Laravel package stechstudio/laravel-zipstream to zip and download the files.
As a result, this post will teach you two techniques for including Laravel zip in your application.
Using Zip Archive class
In this article, we'll zip the Laravel 9 files and then download them. Using the ZipArchive class and a Laravel package called stechstudio/laravel-zipstream, this article will show you how to zip the files and download them.
As a result, this tutorial will teach you how to integrate Laravel zip into your application using two different approaches.
php artisan make:controller ZipController
Next, plan a route. See the code below:
<?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('zip', [ZipController::class, 'index']);
Let's now incorporate the Laravel zip into our controller. The ZipController's whole source code is shown here.
<?php
namespace App\Http\Controllers;
use File;
use ZipArchive;
use Illuminate\Http\Request;
class ZipController extends Controller
{
public function index()
{
$zip = new ZipArchive;
$fileName = 'zipFileName.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
// Folder files to zip and download
// files folder must be existing to your public folder
$files = File::files(public_path('files'));
// loop the files result
foreach ($files as $key => $value) {
$relativeNameInZipFile = basename($value);
$zip->addFile($value, $relativeNameInZipFile);
}
$zip->close();
}
// Download the generated zip
return response()->download(public_path($fileName));
}
}
Note -??make a files folder in your public directory where you can store some assets, such as photographs and PDFs, to test this code.
Let's now run the aforementioned code. Run the command line:
php artisan serve
http://127.0.0.1:8000/zip
You will now download the zip file containing the files you choose.
Using Laravel Zip Package
We are use the stechstudio laravel-zipstream package in this approach. Let's begin by installing this.
composer require stechstudio/laravel-zipstream
Create our controller method now. I called it "method 2"; the code is below.
<?php
namespace App\Http\Controllers;
use File;
use Zip;
use Illuminate\Http\Request;
class ZipController extends Controller
{
public function method2()
{
return Zip::create('zipFileName.zip', File::files(public_path('files')));
}
}
Next, let's plan our route:
<?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('zip-download', [ZipController::class, 'method2']);
Use this package to test our Laravel zip after that.
Open your browser and enter this URL:
http://127.0.0.1:8000/zip-download
You now know two techniques for using Laravel zip. Hope it's helpful. What you employ for our procedures above is entirely up to you.