How To Create ZIP File In Laravel Using ZipArchive

How To Create ZIP File In Laravel Using ZipArchive

In this post we will give you information about How To Create ZIP File In Laravel Using ZipArchive. Hear we will give you detail about How To Create ZIP File In Laravel Using ZipArchiveAnd how to use it also give you demo for it if it is necessary.

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>
<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 >
	<a href="{{ route('create-zip',['download'=>'zip']) }}"  >Download ZIP</a>	
</div>
</body>
</html>

Step : 3 Create Controller

[ADDCODE]

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


namespace AppHttpControllers;

use AppHttpRequests;
use IlluminateHttpRequest;
use ZipArchive;

class ZipArchiveController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return IlluminateHttpResponse
     */
    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…

Hope this code and post will helped you for implement How To Create ZIP File In Laravel Using ZipArchive. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US