Laravel 12 : Creating and Downloading Zip Files

In this tutorial, I’ll demonstrate how to generate and download zip files in a Laravel 12 application. We’ll explore two different approaches to accomplish this task.

Understanding Zip Files

A zip file is a compressed archive format widely used for organizing and reducing the size of files and folders. By compressing one or more items into a single archive, zip files make data more efficient to store and easier to transfer over the internet while preserving the original structure and contents.

Method 1: Using PHP’s ZipArchive Class

Step 1: Set Up a New Laravel 12 Project

Begin by creating a fresh Laravel 12 application:

composer create-project laravel/laravel example-app

Step 2: Create a Route

Add this route to your routes/web.php file:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipController;
    
Route::get('download-zip', ZipController::class);

Step 3: Implement the Controller

Create a controller that handles the zip file creation and download:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
use ZipArchive;
  
class ZipController extends Controller
{
    /**
     * Handle the zip file creation and download
     *
     * @return \Illuminate\Http\Response
     */
    public function __invoke()
    {
        $zip = new ZipArchive;
        $fileName = 'myNewFile.zip';
     
        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {
            $files = File::files(public_path('myFiles'));
     
            foreach ($files as $file) {
                $relativeNameInZipFile = basename($file);
                $zip->addFile($file, $relativeNameInZipFile);
            }
               
            $zip->close();
        }
      
        return response()->download(public_path($fileName));
    }
}

Testing the Implementation

  1. Create a myFiles folder in your public directory and add some files
  2. Start the development server:
php artisan serve
  1. Visit the endpoint in your browser:
http://localhost:8000/download-zip

Method 2: Using the ZipStream Package

Step 1: Install Laravel 12 (if not already done)

composer create-project laravel/laravel example-app

Step 2: Install the ZipStream Package

composer require stechstudio/laravel-zipstream

Step 3: Create a Route

Add the same route as in Method 1 to routes/web.php.

Step 4: Implement the Controller

<?php

namespace App\Http\Controllers;

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

class ZipController extends Controller
{
    /**
     * Handle the zip file creation and download using ZipStream
     *
     * @return \Illuminate\Http\Response
     */
    public function __invoke()
    {
        return Zip::create('zipFileName.zip', File::files(public_path('myFiles')));
    }
}

Testing the Implementation

  1. Ensure you have files in your public/myFiles directory
  2. Start the server:
php artisan serve
  1. Access the endpoint:
http://localhost:8000/download-zip

Both methods will generate a zip file containing all files from your myFiles directory and prompt the user to download it. The ZipArchive method gives you more control over the process, while the ZipStream package offers a more streamlined solution.

Previous Article

Laravel 12 : Creating and Sending Email Notifications

Next Article

Laravel 12 : Implementing Google Maps Autocomplete Address

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨