How To Create ZIP File In Laravel Using ZipArchive

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', 'ZipArchiveController@index')->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>
<html>
<head>
	<title>Create Zip</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
	<a href="{{ route('create-zip',['download'=>'zip']) }}" class="btn btn-info" >Download ZIP</a>	
</div>
</body>
</html>

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
        	$public_dir=public_path();
        	// Zip File Name
            $zipFileName = 'AllDocuments.zip';
            // Create ZipArchive Obj
            $zip = new ZipArchive;
            if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
            	// Add File in ZipArchive
                $zip->addFile(file_path,'file_name');
                // Close ZipArchive     
                $zip->close();
            }
            // Set Header
            $headers = array(
                'Content-Type' => 'application/octet-stream',
            );
            $filetopath=$public_dir.'/'.$zipFileName;
            // Create Download Response
            if(file_exists($filetopath)){
                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);
    }        
    $zip->close();
}

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:


http://localhost:8000/create-zip

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

 Prev post

Next post