How to Create Laravel 8 Zip file and Download?
Tags: laravel, programming, tutorial, laravelzip
Published: 2022-05-12 02:39:30

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/how-to-create-laravel-8-zip-file-and-download

In this post, let's do **Laravel 8 zip files and download** them after we zipped the files. This tutorial will help you to zip the files and download them using the `ZipArchive` class and a Laravel package called `stechstudio/laravel-zipstream`.

 

So you will learn two methods in this post how to implement Laravel zip in your application.

 

## Method #1: Using ZipArchive class

In this method, we will use the PHP ZipArchive class let's loop the selected folder that we attach to our zip file and download it.

 

You need to create a controller first:

```php
php artisan make:controller ZipController
```



Then create a route. See below code:

```php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('zip', [ZipController::class, 'index']);
```
 

Now in our controller let's implement the Laravel zip. Below is the complete code of our ZipController.


```php
<?php

namespace App\Http\Controllers;

use File;
use ZipArchive;
use Illuminate\Http\Request;

class ZipController extends Controller
{
    public function index() 
    {
    	$zip = new ZipArchive;
   
        $fileName = 'zipFileName.zip';
   
        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
        	// Folder files to zip and download
        	// files folder must be existing to your public folder
            $files = File::files(public_path('files'));
   			
   			// loop the files result
            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
            }
             
            $zip->close();
        }
    	
    	// Download the generated zip
        return response()->download(public_path($fileName));
    }
}
```

**Note:** Don't forget to create a files folder to your public directory to test this code and put some files like images and PDFs.

 

Now, let's this our code above. Run the following command:

```php
php artisan serve
```

Then run the following URL:

```php
http://127.0.0.1:8000/zip
```
Now you will download the zip file with files selected.

 

## Method #2: Using Laravel Zip Package
In this method, we are using laravel-zipstream package by stechstudio. Let's install this to start.

```php
composer require stechstudio/laravel-zipstream
```

Then let's create our controller method. I named it method2 see below code:

```php
<?php

namespace App\Http\Controllers;

use File;
use Zip;
use Illuminate\Http\Request;

class ZipController extends Controller
{
    public function method2() 
    {
    	return Zip::create('zipFileName.zip', File::files(public_path('files')));
    }
}
```

Then let's create our route:

```php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('zip-download', [ZipController::class, 'method2']);
```
Then test our Laravel zip using this package.

 

Run the URL to your browser:


```php
http://127.0.0.1:8000/zip-download
```

To learn more about this package just visit the documentation [here](https://github.com/stechstudio/laravel-zipstream).

 

Now, you learn 2 methods when implementing Laravel zip I hope it helps. It's up to you what you will use for our methods above.


I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-create-laravel-8-zip-file-and-download if you want to download this code.

Happy coding :)