Laravel 12 – How to Create and Download ZIP Archives

Touseef Afridi
17 Aug 25

Laravel 12 – How to Create and Download ZIP Archives

In this tutorial, you’ll learn how to create and download ZIP archives in Laravel 12. Compress multiple files into a single ZIP and make them available for easy download in your application.


If you're a video person, feel free to skip the post and check out the video instead!


Quick Overview

This guide walks you through setting up a Laravel project and using the laravel-zipstream package to generate a ZIP file containing images. It begins with creating a fresh Laravel 12 application and selecting the necessary setup options. You'll then install the ZipStream package, which is auto-configured by Laravel without requiring manual registration. After that, you'll create a ZipController to handle the logic for collecting image files from a specific folder and generating a downloadable ZIP archive. A route is added to trigger the downloadZip() method. Once you've created an images folder in the public directory and added a few sample files, you'll test the implementation by launching the Laravel development server and accessing the route to download the ZIP file.

Step # 1 : Set Up a Fresh Laravel 12 Project.

To get started, you can spin up a fresh Laravel 12 installation. Make sure the Laravel installer is installed globally on your system. If not, you can install it using the following command.
composer global require laravel/installer
This command gives you quick access to the laravel command-line tool, making it easier to spin up new projects. Once installed, go ahead and create a fresh Laravel 12 project by running.
laravel new zip
As the setup proceeds, you’ll be guided through several configuration prompts.
  • When asked about the starter kit, select None to keep things minimal.
  • For the testing framework, choose Pest, which is now the default in Laravel 12.
  • When prompted to select a database, go with MySQL (or any other supported database you're comfortable with).
  • You’ll then be asked whether to run the default database migrations type yes to proceed.
  • Lastly, confirm the prompts to run npm install and npm run build so that your frontend assets are properly installed and compiled.

Once the setup is complete, you’ll have a clean Laravel 12 project named zip, ready for package installation and ZIP file generation.

Step # 2 : Navigate to the Project Directory.

Once the project has been created, open your terminal and move into the root folder of your Laravel application. If you created the project inside your local server directory (e.g., XAMPP’s htdocs), the command might look like this.
cd c:xampp/htdocs/zip
Make sure you’re in the correct project directory before continuing. From here, you'll be ready to install the ZIP package and start building the functionality.

Step # 3 : Install the Zipstream Package.

With your Laravel project ready, the next step is to install the laravel-zipstream package. This package makes it easy to create and stream ZIP files directly from your application. To install it, run the following command in your project directory.
composer require stechstudio/laravel-zipstream
Composer will handle the installation and pull in all required dependencies. There’s no need to manually register any service providers or facades, Laravel 12 takes care of that automatically. Once installed, the package is ready to use right out of the box.

Step # 4 : (Optional) Publish the Configuration File.

The laravel-zipstream package works out of the box with sensible defaults. However, if you’d like to fine-tune its behavior, such as adjusting the buffer size or setting a default filename, you can publish the package’s configuration file. To do that, run the following Artisan command.
php artisan vendor:publish --provider="STS\ZipStream\ZipStreamServiceProvider"
This will create a zipstream.php file inside your project’s config directory. Open the file to explore various settings you can customize to match your use case.

Step # 5 : Create the ZipController.

Now that the package is installed and optionally configured, it’s time to build the controller that will handle the ZIP file generation. Run the following Artisan command to create a new controller.
php artisan make:controller ZipController
Once the controller is created, open ZipController.php located in the app/Http/Controllers directory, and replace its contents with the following code.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use STS\ZipStream\Facades\Zip;
class ZipController extends Controller
{
    public function downloadZip()
    {
        // Path to the 'images' folder in the public directory
        $imagesPath = public_path('images');
        // Collect all files from the folder
        $files = [];
        foreach (File::files($imagesPath) as $file) {
            $files[] = $file->getPathname();
        }
        // Generate and return the ZIP file as a download
        return Zip::create('images.zip', $files);
    }
}
The downloadZip method defines the path to the images directory inside the public folder, then loops through all the files in that directory to collect their full paths in an array. It uses the Zip::create() method provided by the laravel-zipstream package to generate a ZIP file from those files and returns it as a downloadable response. With the controller in place, you're now ready to move forward and test the ZIP file generation.

Step # 6 : Define the Route.

With the controller logic in place, the next step is to define a route that triggers the ZIP download functionality. Open the routes/web.php file and add the following
use App\Http\Controllers\ZipController;
Route::get('/zip', [ZipController::class, 'downloadZip']);
This route maps the /zip URL to the downloadZip method in the ZipController. When a user visits this URL in the browser, the method will execute, and a ZIP file containing all images from the public/images folder will be generated and downloaded automatically.

Step # 7 : Prepare the Images Folder.

Before testing the ZIP functionality, make sure there are files available to compress. Navigate to the public directory of your Laravel project and create a new folder named images. Inside this folder, add a few image files, such as .jpg, .png, or any other file types you’d like to include in the ZIP archive. These files will be used by the downloadZip method when generating the ZIP file.

Step # 8 : Test the ZIP Download.

Now it’s time to test everything you’ve set up. Start the Laravel development server by running the following command in your terminal.
php artisan serve
Once the server is running, open your browser and visit: http://127.0.0.1:8000/zip. This will trigger the downloadZip method, generate the ZIP file, and prompt a download.

After the download is complete, extract the ZIP file, you should see all the image files you placed inside the public/images folder.

Conclusion

By following this guide, you’ve successfully integrated the laravel-zipstream package into your Laravel project, making it easy to generate and download ZIP files containing images. You created a dedicated controller, defined a route to handle the download, and tested the functionality using sample files. This approach provides a straightforward way to deliver compressed content to users, and if needed, you can further tailor the behavior by modifying the configuration file.
For more options and advanced usage, be sure to explore the official Laravel ZipStream documentation.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering