Let's get the Laravel 9 zip files in this post when we've zipped the files. This guide will show you how to use the ZipArchive class and the Laravel package stechstudio/laravel-zipstream to zip and download the files.

As a result, this post will teach you two techniques for including Laravel zip in your application.

Using Zip Archive class

In this article, we'll zip the Laravel 9 files and then download them. Using the ZipArchive class and a Laravel package called stechstudio/laravel-zipstream, this article will show you how to zip the files and download them.

As a result, this tutorial will teach you how to integrate Laravel zip into your application using two different approaches.

php artisan make:controller ZipController

Next, plan a route. See the code below:

<?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']);

Let's now incorporate the Laravel zip into our controller. The ZipController's whole source code is shown here.

<?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 -??make a files folder in your public directory where you can store some assets, such as photographs and PDFs, to test this code.

Let's now run the aforementioned code. Run the command line:

php artisan serve
http://127.0.0.1:8000/zip

You will now download the zip file containing the files you choose.

Using Laravel Zip Package

We are use the stechstudio laravel-zipstream package in this approach. Let's begin by installing this.

composer require stechstudio/laravel-zipstream

Create our controller method now. I called it "method 2"; the code is below.

<?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')));
    }
}

Next, let's plan 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']);

Use this package to test our Laravel zip after that.

Open your browser and enter this URL:

http://127.0.0.1:8000/zip-download

You now know two techniques for using Laravel zip. Hope it's helpful. What you employ for our procedures above is entirely up to you.


Recommended Posts

View All

Generate Laravel PDF with Image Example


In this post, I will demonstrate how to create a Laravel PDF with an image. We occasionally need to generate a PDF with an image for reporting purpose...

Laravel 9 Autocomplete Search using Typeahead JS Tutorial


Laravel 8 autocomplete search using Typehead JS from MySQL database example

How to Get Current URL in Laravel


how to get current url in laravel, laravel get current url, get current page url in laravel, get full url in laravel, find current url in laravel,

Laravel 9 PHP Guzzle Http Client Examples


We will provide answers in this manual. If you've been searching the internet for a Laravel Guzzle http client example, your search is over.

Laravel 8 How To Merge Two PDF Files Example


laravel 8 how To merge two PDF files example,how to merge two PDF files in laravel 8,merge two PDF files,pdf,pdf in laravel 8