How to create zip file using zipArchive in Laravel6

  6 months ago     Views:  481  
Site Admin
4 Follows


Hello, everyone in this article we will share with you 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', 'CreateZipController@index')->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..


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

No Comments