Table of contents
In this post, let's do Laravel 9 zip files and download them after we zipped the files. This tutorial will help you to zip the files and download them using the ZipArchive
class and a Laravel package called stechstudio/laravel-zipstream
.
Â
So you will learn two methods in this post how to implement Laravel zip in your application.
Â
Method #1: Using ZipArchive class
In this method, we will use the PHP ZipArchive class let's loop the selected folder that we attach to our zip file and download it.
Â
You need to create a controller first:
php artisan make:controller ZipController
Â
Then create a route. See the below code:
<?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']);
Â
Now in our controller let's implement the Laravel zip. Below is the complete code of our ZipController.
<?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: Don't forget to create a files folder in your public directory to test this code and put some files like images and PDFs.
Â
Now, let's this our code above. Run the following command:
php artisan serve
Â
Then run the following URL:
http://127.0.0.1:8000/zip
Â
Now you will download the zip file with the files selected.
Â
Method #2: Using Laravel Zip Package
In this method, we are using laravel-zipstream
package by stechstudio. Let's install this to start.
composer require stechstudio/laravel-zipstream
Â
Then let's create our controller method. I named it method2 see below code:
<?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')));
}
}
Â
Then let's create 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']);
Â
Then test our Laravel zip using this package.
Â
Run the URL to your browser:
http://127.0.0.1:8000/zip-download
Â
To learn more about this package just visit the documentation here.
Â
Now, you learn 2 methods when implementing Laravel zip I hope it helps. It's up to you what you will use for our methods above.
Read next