Latest Code Tutorials

How to Create and Extract zip File in Laravel


How to Create and Extract zip File in Laravel is the topic, we will discuss today. In this example, we use laravel 5.6. We use chumper/zipper package for creating and extract the zip file in laravel. The process of “zipping” a file refers to putting it into an archive format such as “ZIP.” It compresses the file, making it smaller. You can squeeze several files into a single ZIP archive, making them easier to store and transfer over an email as the files are smaller. They can be moved by just moving that one ZIP file. So it works as a single file and not a whole folder. If you need to move many files or huge ones, then zipping them may be the best way to go.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Start your free trial

How to Create and Extract zip File in Laravel

First, we install new Laravel Project.

Step 1: Install Laravel Project

Download laravel project by typing following command in cmd.

composer create-project --prefer-dist laravel/laravel laravelzip

Step 2: Install chumper/zipper Package

We will install chumper/zipper package by hit following command in cmd.

composer require chumper/zipper

Step 3: Define providers and aliases

Find the providers in config >> app.php file and register the ZipperServiceProvider.

'providers' => [
        // ...

Locate the aliases in config >> app.php file and designate the aliases.

'aliases' => [
        // ...
        'Zipper' => 'Chumper\Zipper\Zipper',

Step 4: Create a Controller

php artisan make:controller DataController --resource

It will create one controller file called DataController.php.

Step 5: Create Zip File in Laravel

Next, we create Zip File by typing following code in Controller.

public function create()
        $files = glob(public_path('js/*'));
        return response()->download(public_path(''));

If you want to zip the whole project, then you change the path.

 public function create()
        $files = base_path(); 
        return response()->download(public_path(''));

The file is downloaded your public directory.

How to Create zip File in Laravel

Step 6: Extract Zip File in Laravel

We have already created the file so we can easily extract from the public directory.

public function index()
        $Path = public_path('');

You can see that create a folder in public directory called Appdividend. This folder contains extract zip file.

How to Extract zip File in Laravel

Step 7: Build Route

we register route in routes  >>  web.php file. So let us do it.


Route::get('index', 'DataController@index');

We define two routes. The first route for the creates a zip File. The second route for the extract zip file.

Start Laravel Development by type following command.

 php artisan serve

Type URL on your browser: http://localhost:8000/create

Possible Error:

The maximum execution time of 60 seconds exceeded.

Possible Solution: 

Add following line in the create() and index() function.

ini_set('max_execution_time', 3000);

Finally, Our How to Create and Extract zip File in Laravel is over. Thanks for taking.

  1. akshay says

    I have used chumper/zipper package the code works fine but when i download the zipped archive i am getting no archive found. is there any solution to this or any other package. Suggestions would be helpful,thank you.

    Size of zipped files:


  2. Mike says

    I enjoy the article

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.