---
description: >-
  https://www.itsolutionstuff.com/post/how-to-create-zip-file-and-download-in-laravel-6example.html
---

# How to Create Zip File and Download in Laravel 7/6?

## How to Create Zip File and Download in Laravel 7/6?

By Hardik Savani September 27, 2019 Category : Laravel![](https://a.vdo.ai/core/assets/img/cross.svg)PlayUnmuteLoaded: 1.15%Fullscreen[![VDO.AI](https://a.vdo.ai/core/assets/img/logo.svg)](https://vdo.ai/?utm_medium=video\&utm_term=itsolutionstuff.com\&utm_source=vdoai_logo)[![](https://go.ezodn.com/charity/http/charity-ads.s3.amazonaws.com/charity_ads/1079/728x90.png)](https://go.ezodn.com/ads/charity/proxy?p_id=af1af8c6-368e-4170-4f91-9dfd5f4d2791\&d_id=77568\&imp_id=7841461320751037\&c_id=1079\&l_id=10016\&url=https%3A%2F%2Fwww.directrelief.org%2Femergency%2Fukraine-crisis%2F\&ffid=1\&co=VN)In this tutorial, i am writing example of laravel 7/6 create zip archive file and download in response. i will give you example step by step how to create zip file from folder and download in laravel 7/6. we will create zip file using ziparchive class in php laravel 7/6 application.

I also post few months ago how to create zip file using composer package in laravel 5. if you want to create zip file using another composer package then you can follow this tutorial: [Laravel 5 create and download zip file example using zipper](https://itsolutionstuff.com/post/laravel-5-create-and-download-zip-file-example-using-chumper-zipper-composer-packageexample.html).

But i will highly recommended to create zip archive file without using anymore composer package, because you can easily make zip file using php ziparchive class. ziparchive class provide method to add files so you can easily add files with relative path, so you can easily create folder inside your zip file.

In this post, i will show you how to create very simple way to zip file in laravel 6 application. So let's follow few things and make it simple example.

![](https://www.itsolutionstuff.com/upload/laravel-6-create-zip-file.png)

**Step 1: Create Route**

First thing is we put one route in one for download created zip file. So simple add both routes in your route file.

routes/web.php

```
Route::get('download-zip', 'ZipController@downloadZip');
```

**Step 2: Create Controller**

Same things as above for route, here we will add one new method for route. downloadZip() will generate new zip file and download as response, so let's add bellow:

app/Http/Controllers/ZipController.php

```
<?php   namespace App\Http\Controllers;   use Illuminate\Http\Request;use File;use ZipArchive;  class ZipController extends Controller{    /**     * Display a listing of the resource.     *     * @return \Illuminate\Http\Response     */    public function downloadZip()    {        $zip = new ZipArchive;           $fileName = 'myNewFile.zip';           if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)        {            $files = File::files(public_path('myFiles'));               foreach ($files as $key => $value) {                $relativeNameInZipFile = basename($value);                $zip->addFile($value, $relativeNameInZipFile);            }                         $zip->close();        }            return response()->download(public_path($fileName));    }}
```

Ok now you can run project and open that route like.

But make sure you have "myFiles" folder in public directory and add some pdf files on that file so it will create zip file with those files.

Now you can test it by using following command:

```
php artisan serve
```

Now you can open bellow URL on your browser:

Read Also: [How to Send Mail in Laravel 6?](https://www.itsolutionstuff.com/post/how-to-send-mail-in-laravel-6example.html)

```
http://localhost:8000/download-zip
```

I hope it can help you...
