DEV Community

Cover image for How to Create Laravel 8 Zip file and Download?
Code And Deploy
Code And Deploy

Posted on

How to Create Laravel 8 Zip file and Download?

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/how-to-create-laravel-8-zip-file-and-download

In this post, let's do Laravel 8 zip files and download them after we zipped the files. This tutorial will help you to zip the files and download them using the ZipArchive class and a Laravel package called stechstudio/laravel-zipstream.

So you will learn two methods in this post how to implement Laravel zip in your application.

Method #1: Using ZipArchive class

In this method, we will use the PHP ZipArchive class let's loop the selected folder that we attach to our zip file and download it.

You need to create a controller first:

php artisan make:controller ZipController
Enter fullscreen mode Exit fullscreen mode

Then create a route. See below code:

<?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('zip', [ZipController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

Now in our controller let's implement the Laravel zip. Below is the complete code of our ZipController.

<?php

namespace App\Http\Controllers;

use File;
use ZipArchive;
use Illuminate\Http\Request;

class ZipController extends Controller
{
    public function index() 
    {
        $zip = new ZipArchive;

        $fileName = 'zipFileName.zip';

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
            // Folder files to zip and download
            // files folder must be existing to your public folder
            $files = File::files(public_path('files'));

            // loop the files result
            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);
            }

            $zip->close();
        }

        // Download the generated zip
        return response()->download(public_path($fileName));
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: Don't forget to create a files folder to your public directory to test this code and put some files like images and PDFs.

Now, let's this our code above. Run the following command:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Then run the following URL:

http://127.0.0.1:8000/zip
Enter fullscreen mode Exit fullscreen mode

Now you will download the zip file with files selected.

Method #2: Using Laravel Zip Package

In this method, we are using laravel-zipstream package by stechstudio. Let's install this to start.

composer require stechstudio/laravel-zipstream
Enter fullscreen mode Exit fullscreen mode

Then let's create our controller method. I named it method2 see below code:

<?php

namespace App\Http\Controllers;

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

class ZipController extends Controller
{
    public function method2() 
    {
        return Zip::create('zipFileName.zip', File::files(public_path('files')));
    }
}
Enter fullscreen mode Exit fullscreen mode

Then let's create our route:

<?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('zip-download', [ZipController::class, 'method2']);
Enter fullscreen mode Exit fullscreen mode

Then test our Laravel zip using this package.

Run the URL to your browser:

http://127.0.0.1:8000/zip-download
Enter fullscreen mode Exit fullscreen mode

To learn more about this package just visit the documentation here.

Now, you learn 2 methods when implementing Laravel zip I hope it helps. It's up to you what you will use for our methods above.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-create-laravel-8-zip-file-and-download if you want to download this code.

Happy coding :)

Top comments (1)

Collapse
 
jurajchovan profile image
Juraj Chovan

hi friend,
if I want download some file, then delete it, I can use command:

return response()->download($SomeFileName)->deleteFileAfterSend(true);
Enter fullscreen mode Exit fullscreen mode

but open question is:
if I want download some file, then delete it and then redirect Laravel application to some other page/route?
What is code for this case? Can you help me pls ...