AppDividend
Latest Code Tutorials

How to Create and Extract zip File in Laravel

1,143

Get real time updates directly on you device, subscribe now.

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.

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' => [
        // ...
       'Chumper\Zipper\ZipperServiceProvider',
    ]

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/*'));
        \Zipper::make(public_path('test.zip'))->add($files)->close();
        return response()->download(public_path('test.zip'));
    }

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

 public function create()
    {
        $files = base_path(); 
        \Zipper::make(public_path('test.zip'))->add($files)->close();
        return response()->download(public_path('test.zip'));
    }

The test.zip 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 test.zip file so we can easily extract from the public directory.

public function index()
    {
        $Path = public_path('test.zip');
        \Zipper::make($Path)->extractTo('Appdividend');
    }

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.

//web.php

Route::get('create','DataController@create');
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:

Related Posts
1 of 37

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.

2 Comments
  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: https://pasteboard.co/Hpz00p7.png

    afterdownloading: https://pasteboard.co/HpyYDQ3.png

  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.