How to create zip file using zipArchive in Laravel 6

How to create zip file using zipArchive in Laravel 6

In this article, you'll learn how to create a zip file using zipArchive class. In many Laravel application, you need to create functionality to many of file's make in one zip file and then download it.

You can find many Laravel packages which provide you zip create functionality and easy to use. But in this article we will share with you how to create zip file help of zipArchive class. Here no need to install any extra packages.

What is zipArchive ?

zipArchive is one of the php's class. help of this class and methods you can easy to work with zip related all tasks and functionality. like, create zip, add a file to zip, add the directory in zip, add more than one directory in zip, etc... for more information please visit this link zipArchive Class.

Step - 1 Create route

Now, we first need to create one route in routes/web.php file.

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

Step - 2 Create controller

After creating route then we need to create also CreateZipController controller using the following command run in your terminal or cmd.

php artisan make:controller CreateZipController

After run this commant your CreateZipController.php file automatic created on app/Http/Controllers folder. just open it and write following code into that file.

<?php
namespace App\Http\Controllers;

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

class CreateZipController 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');
    }
}

How to add more file in zip

if you want to add more then one files or multiple files in created zip then you can use following code snippet for adding more in in zipArchive class.

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

Step - 3 Create blade file

Into the last, we need to create one blade file which we use simple HTML layout for creating a zip file. so, in this blade file, we simply add one create zip button HTML code. but you can do it depends on your own functionality.

<!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>

Conclusion

As you can see, create zip file in laravel application is very easy help of using zipArchive class. Here we don't need to use any laravel packages for create zip file.

We are hope this tutorials help everyone. if you have any issues or question reagarding create zip using of zipArchive so please comment bellow. Thanks..

laravel php web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...