How to create zip files in Laravel?

2021-04-18 17:17:45 Uttkarsh Laravel

In this tutorial we are going to see how to create zip files in Laravel Framework.

We are going to use zanysoft/laravel-zip package for file compression and backup. was zanysoft/laravel-zip package is written to simplify and automate Zip files management using PHP ZipArchive.

Let's start...

Step1: Cd to your projexct directory.

Step2: Run below commnad to install the laravel-zip package.

composer require zanysoft/laravel-zip

Step3: Add ZanySoft\Zip\ZipServiceProvider::class, ServiceProvider to the providers array in config/app.php.

Step4: If you want to use facade then add 'Zip' => ZanySoft\Zip\ZipFacade::class, in your your facades array.

Installation of laravel-zip package id done.

Zip usage: Use below to code in your controller to start zipping your files and folders in Laravel.

use Zip;
$zip = Zip::create('');
$zip->add('/path/to/my/file'); // adding a file in zip.
$zip->add('/path/to/my/directory'); // adding an directory.

This post is submitted by one of our members. You may submit a new post here.

Related Tricks