Create Zip File and Download in Laravel 9


Share:
 | Published By: Admin | Published On: Apr 05, 2022 | Category: Laravel 9

Hi friends,

Today, I explain create zip file and download in laravel 9. In this tutorial, I am writing an example of laravel 9 create zip archive file and download it in response. we will create a zip file using the zip-archive class in php laravel 9 application.

I will give you an example step by step how to create a zip file from a folder and download in laravel 9 application. i will give you example step by step how to create zip file from folder and download in laravel 9 application. we will create zip file using ziparchive class in php laravel 9 application.

In this post, i will show you how to create very simple way to zip file in laravel 9 application. So let's follow few things and make it simple example.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Route

First thing is we put one route in one for download created zip file. So simple add both routes in your route file.

routes/web.php
<?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('download-zip', [ZipController::class, 'downloadZip']);
Step 3: Create Controller
php artisan make:controller ZipController

Same things as above for route, here we will add one new method for route. downloadZip() will generate new zip file and download as response, so let's add bellow:

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 downloadZip()
    {
        $zip = new ZipArchive;
   
        $fileName = 'myNewFile.zip';
   
        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
            $files = File::files(public_path('myFiles'));
   
            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
            }
             
            $zip->close();
        }
    
        return response()->download(public_path($fileName));
    }
}
But make sure you have "myFiles" folder in public directory and add some pdf files on that file so it will create zip file with those files. Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/download-zip

I hope it can help you...

Share: