How To Create ZIP File In Laravel Using ZipArchive

  391677 views   5 years ago Laravel

Hello, Everyone in this tutorials we are share with you how to create zip in laravel usgin ZipArchive. in may project you need to create functionality to some project or application file's make one zip and download all in zip file.

You are fine many laravel packages which are also provide this functionality. but in this article we are not use any package. we are use ZipArchive class for create a zip file in laravel.

How to done create zip functionality with ZipArchive class? we are show you step by step. please simply follow this step.

Step : 1 Create Route

First we are create one route for show simple Create ZIP Button

Route::get('create-zip', '[email protected]')->name('create-zip');

Step : 2 Create Simple Button Blade

Now we are create one blade file. in this blade file we are simple show one Zip Download bootstrap button and when user click on it and they able to download some files and images in zip file which we are already chooze for make zip.

<!DOCTYPE html>
	<title>Create Zip</title>
	<link rel="stylesheet" type="text/css" href="">
<div class="container">
	<a href="{{ route('create-zip',['download'=>'zip']) }}" class="btn btn-info" >Download ZIP</a>	

Step : 3 Create Controller


It is last and final step we are create ZipArchiveController and simple put following code in it.

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use ZipArchive;

class ZipArchiveController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function index(Request $request)
        if($request->has('download')) {
        	// Define Dir Folder
        	// Zip File Name
            $zipFileName = '';
            // Create ZipArchive Obj
            $zip = new ZipArchive;
            if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
            	// Add File in ZipArchive
                // Close ZipArchive     
            // Set Header
            $headers = array(
                'Content-Type' => 'application/octet-stream',
            // Create Download Response
                return response()->download($filetopath,$zipFileName,$headers);
        return view('createZip');

NOTE : If youe want to add more then one file in zip. so please use following code snippet for add file in ZipArchive

if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {    
    // Add Multiple file   
    foreach($files as $file) {
        $zip->addFile($file->path, $file->name);

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:


If you face any problem then please write a comment or give some suggestions for improvement. Thanks...

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]