Laravel 9- How to Create Zip File And Download Step by Step

File compression and downloading are essential features in web development, often used for packaging multiple files into a single convenient format. Laravel, being a versatile PHP framework, provides a straightforward and efficient way to create zip files and offer them for download to users. In this comprehensive guide, we will walk you through the step-by-step process of creating zip files and enabling smooth downloads in Laravel 9. Armed with practical examples, you’ll be equipped to implement this powerful functionality in your own projects with ease.

Step 1: Setting Up a New Laravel Project

Before we dive into creating and downloading zip files, let’s ensure you have Laravel 9 installed in your local development environment. If not, follow these simple steps to create a new Laravel project using Composer:

composer create-project laravel/laravel laravel-zip-demo
cd laravel-zip-demo

Step 2: Install the Required Package

In Laravel, we’ll use the “zipper” package to handle zip file creation and manipulation. Install this package via Composer:

composer require chumper/zipper

Step 3: Create a Route and Controller Method

In your Laravel project, create a new route and corresponding controller method that will handle the zip file creation and download:

web.php

use App\Http\Controllers\ZipController;

Route::get('/create-zip', [ZipController::class, 'createZip']);

ZipController.php

namespace App\Http\Controllers;

use Zipper;

class ZipController extends Controller
{
    public function createZip()
    {
        // Code for creating the zip file will be added here
    }
}

Step 4: Add Files to Zip and Download

In the createZip method of the ZipController, we’ll use the “zipper” package to add files to the zip and enable the user to download it:

public function createZip()
{
    $files = [
        public_path('files/document1.txt'),
        public_path('files/document2.txt'),
        public_path('files/image.jpg'),
    ];

    $zipFileName = 'my_zip_file.zip';

    Zipper::make(public_path($zipFileName))->add($files)->close();

    return response()->download(public_path($zipFileName))->deleteFileAfterSend();
}

Step 5: Route to Test the Download

Create a new folder named “files” inside the “public” directory and place some sample files (e.g., document1.txt, document2.txt, and image.jpg) inside it. Now, navigate to the /create-zip route in your browser. You should see the zip file being created and downloaded automatically.

Bonus: Adding Folders to the Zip

If you want to include entire folders in the zip file, you can use the “addFolder” method:

public function createZip()
{
    $folderPath = public_path('files/documents');

    $zipFileName = 'my_zip_file_with_folders.zip';

    Zipper::make(public_path($zipFileName))->addFolder($folderPath)->close();

    return response()->download(public_path($zipFileName))->deleteFileAfterSend();
}

Conclusion

Congratulations! You’ve successfully learned how to create zip files and enable downloads in Laravel 9 using the “zipper” package. With this knowledge, you can now implement this feature in various scenarios, such as bundling multiple files for user downloads or backing up essential data in a single zip file. Laravel’s flexibility and the “zipper” package’s simplicity make this task a breeze. Feel free to experiment and extend this functionality to suit your specific project requirements. Happy coding and zipping!