How to Create and Download Zip File using Laravel 10?

Spread the love

Hello Guys,

In this tutorial, we will delve into the process of create and download zip file in Laravel 10. If you’re looking for a practical example of how to generate a zip file in Laravel 10, you’ve come to the right place. We will cover two methods for achieving this: using the “stechstudio/laravel-zipstream” package and the built-in ZipArchive feature.

Why Zip Files Matter

Zip files are a popular solution for efficiently organizing and compressing substantial volumes of data. This method is widely embraced for reducing the size of large files or folders, making them easier to share and store. By compressing multiple files or folders into a single archive, zip files maintain their original structure and content. As a result, they are not only more convenient for online sharing and downloading but also occupy less space during transfer and storage.

Creating Zip Files in Laravel 10

In this article, we’ll explore two approaches to create zip files in Laravel 10:

  1. Using the “stechstudio/laravel-zipstream” package.
  2. Leveraging the ZipArchive functionality.

In both cases, we will start by creating a folder named “myFiles” and then generate a zip file from its contents. Let’s delve into each of these methods individually.

Example 1 Create and Download Zip File:

Using the ZipArchive class, let’s examine this example.

Also Read: How to use One to One Relationship in LARAVEL 10

Step 1: Create Route

In this stage, we will outline the process of establishing a pathway for generating and downloading zip files. To achieve this, follow the steps below to add the requisite route within your “routes/web.php” file. This is a crucial step for optimizing your website’s functionality and ensuring seamless zip file handling.

routes/web.php
<?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('download-zip', ZipController::class);

Step 2: Create Controller

Just as mentioned earlier regarding the route, we are about to introduce a new method for this route. Since the __invoke() function will generate a fresh zip file and provide it for download as a response, please incorporate the following steps:

Also Read: How to add Custom Validation Error Message in LARAVEL 10

app/Http/Controllers/ZipController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use File;
use ZipArchive;
  
class ZipController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @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 $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
            }
               
            $zip->close();
        }
      
        return response()->download(public_path($fileName));
    }
}

That route can now be opened and the project can be run.

To ensure that a zip file containing those files is created, place the “myFiles” folder in the public directory and add some pdf files to it.

Example 2:

Let’s examine this example using the composer package for stechstudio/laravel-zipstream.

Also Read: How to use Try Catch Exception Handling in API Using laravel 10

Step 1: Install stechstudio/laravel-zipstream Package

composer require stechstudio/laravel-zipstream
app/Http/Controllers/ZipController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use Zip;
class ZipController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __invoke()
    {
        return Zip::create('zipFileName.zip', File::files(public_path('myFiles')));
    }
}

You can now access the following Address in your browser:

Also Read: how to integrate chatgpt API in laravel 10

http://localhost:8000/download-zip or http://127.0.0.1:8000/download-zip

Explore our Laravel blog section for insightful content and helpful tips.

Leave a Comment