How to Create Zip File and Download Using Laravel Zipstream in Laravel 10

  Mar 2024
  ITSolutionsGuides
  Category: Laravel
How to Create Zip File and Download Using Laravel Zipstream in Laravel 10

Welcome To ITSolutionsGuides,


In Laravel 10, managing file downloads efficiently is crucial for enhancing user experience, and one effective way to streamline this process is by creating and downloading zip files. With Laravel Zipstream, developers can achieve this seamlessly. To begin, ensure Laravel Zipstream is installed in your project using Composer. Next, create a controller method to handle the zip file creation and download, specifying the files to include in the zip and the desired zip file name. Define a route to this controller method in your routes/web.php file. Trigger the download by accessing the defined route, typically from a view or another controller method. You can customize the controller method to dynamically fetch files from the database or adjust file paths according to your project structure. Additionally, implement authentication or authorization checks before allowing downloads to ensure security. By following these steps, you can effortlessly create and download zip files in Laravel 10 using Zipstream, improving the efficiency of file management within your application and providing users with a seamless downloading experience.
Lets use Laravel 10's Zipstream to effortlessly create and download zip files. With Zipstream, crafting zip archives from folders becomes seamless. Generate zip files with ZipArchive in Laravel 10 and deliver them for download with a swift response.

Let's Start Coding

Lets instal the new laravel project and then lets install the stechstudio/laravel-zipstream package to create the zip file , lets install stechstudio/laravel-zipstream package using the below command ,

composer require stechstudio/laravel-zipstream

Let's Create Routes

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);

Let's Create Controller

Lets create the controller using the following artisan command
php artisan make:controller ZipController

for detailed explanation about creating the controller see here,

We will use the Zip::create() class in the laravel.


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('itsolutionsguides.zip', File::files(public_path('myFiles')));
    }
}

Let's Run Project

Lets run the project using the following artisan command,

php artisan serve

then browse the below url in the browser.

http://localhost:8000/download-zip

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides