Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Extract/Unzip a Zip File in Laravel

How to Extract/Unzip a Zip File in Laravel ?

Aman Jain, May 22, 2022November 5, 2023

In laravel Extract/Unzip zip a file can be achieved by ZipArchive library,its gives easy to use flexibilities to developers so they can easily Extract/Unzip a Zip File in Laravel and integrate the Zip creation activity in the project. Zip files are used to create lossless data compression and store multiple files folder in single file. So in this article i will show you to Extract/unzip zip file to the specific folder in laravel.

In this article i will use PHP ZipArchive library to extract the zip. This library has several methods to open, create , extract, add, setPath, delete and many more. But in this topic we will cover extracting the zip file.

In this example we will create a simple example to to upload the zip file and then extract it to specific folder . For this we will create a controller, view and logic for extract the zip.

Basic step to Extract/Unzip a Zip File in Laravel

Let’s start Extract/Unzip a Zip File in Laravel with simple step by step

Step 1: Create a fresh laravel project

Open a terminal window and type below command to create a new project

composer create-project --prefer-dist laravel/laravel blog

You can also read this to start with new project

Step 2 : Create controller

Let’s create a controller and add methods extractUploadedZip and zipUploadForm

php artisan make:controller ZipController

and add the below code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ZipArchive;
class ZipController extends Controller
{
    
   
    function zipUploadForm(Request $request){
         
        return view("unzip");
    }
    function extractUploadedZip(Request $request){
         
        $zip = new ZipArchive();
        $status = $zip->open($request->file("zip")->getRealPath());
        if ($status !== true) {
         throw new \Exception($status);
        }
        else{
            $storageDestinationPath= storage_path("app/uploads/unzip/");
       
            if (!\File::exists( $storageDestinationPath)) {
                \File::makeDirectory($storageDestinationPath, 0755, true);
            }
            $zip->extractTo($storageDestinationPath);
            $zip->close();
            return back()
             ->with('success','You have successfully extracted zip.');
        }
    }

}

In above code zipUploadForm method is used to show the form and extractUploadedZip method is used to extract the uploaded zip in the target folder .

extractUploadedZip we have initialized the ZipArchive object then opened the uploaded file. If its okay then it will process the zip file and using the extractTo method it will extract the zip to the target location.

function extractUploadedZip(Request $request){
         
        $zip = new ZipArchive();
        $status = $zip->open($request->file("zip")->getRealPath());
        if ($status !== true) {
         throw new \Exception($status);
        }
        else{
            $storageDestinationPath= storage_path("app/uploads/unzip/");
       
            if (!\File::exists( $storageDestinationPath)) {
                \File::makeDirectory($storageDestinationPath, 0755, true);
            }
            $zip->extractTo($storageDestinationPath);
            $zip->close();
            return back()
             ->with('success','You have successfully extracted zip.');
        }
    }

Step 4 : Create View File

Now, show the upload form in view file

resources/views/unzip.blade.php

<!DOCTYPE html>
<html>

<head>
    <title>How to Extract/Unzip a Zip File in Laravel- readerstacks.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>

<body>
    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h2>How to Extract/Unzip a Zip File in Laravel -readerstacks.com</h2>
            </div>
            <div class="panel-body">

                @if ($message = Session::get('success'))
                <div class="alert alert-success alert-block">
                    <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>{{ $message }}</strong>
                </div>

                @endif
                <br><br>
                <form action="{{ url('extract-zip') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                    <div class="row">

                       
                        <div class="col-md-4">
                            <label>Zip</label>
                            <input type="file" placeholder="ZIP" name="zip">
                        </div>
                    </div>
                    <div class="row">



                        <div class="col-md-6">
                            <button type="submit" class="btn btn-success">Upload</button>
                        </div>

                    </div>
                </form>
                
            </div>
        </div>
    </div>
</body>

</html>

Step 3: Create two routes in routes/web.php

One route for show the zip form and other to extract the zip .

routes/web.php

<?php

use App\Http\Controllers\ZipController;
use Illuminate\Support\Facades\Route;

Route::get("/zip-form",[ZipController::class,"zipUploadForm"]);
Route::post("/extract-zip",[ZipController::class,"extractUploadedZip"]);

I

Results Screenshot:

Extract zip in laravel
Extract zip in laravel
Extract zip in laravel
Extract zip in laravel

I hope it will help you to implement to unzip uploaded file. you can also read article to How to Create Zip of File or Nested Folder in Laravel ?

Related

Php Laravel Laravel 9 extractfilesfolderlaravelunzipzip

Post navigation

Previous post
Next post

Related Posts

Php Creating controller in laravel

How to create controller in Laravel 8?

November 7, 2021November 5, 2023

In Laravel controller is most important part to create the connection between our business logic to our view. we will learn create controller in laravel 8.Laravel is a MVC pattern and C stands for controller. It’s responsible for to receive the input from user, process them and validate the input…

Read More
Php How to Filter Data Using Relational Model in Laravel

How to Filter Data Using Relational Model in Laravel ?

June 29, 2022July 1, 2022

In this article i will show you to filter data using the relational model in laravel. Using the laravel relationship between the models we can easily fetch relative data but sometimes we want to fetch the data by filtering the child model that affect the result of parent model. You…

Read More
Php Static keyword

What is static keyword and properties in a php/java class?

June 13, 2021September 21, 2021

Static keyword are isolated, which means we can access the property of a class without creating a object/instance of class. static methods that are common to all the objects of the class. Hence, any logic which can be shared among multiple instances of a class should be inside the static…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version