How To Create zip File And Download In Laravel 8




How To Create zip File And Download In Laravel 8

Hello Friends,

In this tutorial, I am writing an example of laravel 8 create zip archive file and download it in response. I will give you an example step by step how to create a zip file from a folder and download in laravel 8. we will create a zip file using the zip-archive class in php laravel 8 application.

But I will highly be recommended to create a zip archive file without using anymore composer package because you can easily make a zip file using PHP zip-archive class. the zip-archive class provides a method to add files so you can easily add files with a relative path, so you can easily create the folder inside your zip file.

In this post, I will show you how to create a very simple way to zip files in laravel application. So let’s follow few things and make it a simple example.

Step 1 : 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.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ZipController;

Route::get('download-zip', [ZipController::class, 'downloadZip']);

Step 2 : Create Controller

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:



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 = '';

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);




return response()->download(public_path($fileName));



Ok now you can run project and open that route like.

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.

Now you can test it by using following command:

php artisan serve

Now you can open bellow URL on your browser:


I hope it can help you...

#Laravel 8

#Laravel 7


#Laravel 6