Table of contents

laravel zip

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 artisan make:controller ZipController

 

Then create a route. See below code:

<?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

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 artisan serve

 

Then run the following URL:

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.

composer require stechstudio/laravel-zipstream

 

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

<?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

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:

http://127.0.0.1:8000/zip-download

 

To learn more about this package just visit the documentation here.

 

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.