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 Laravel match password with hash string

How to check password with hashed string in laravel 8 ?

January 26, 2022January 26, 2022

Laravel hashed password with bcrypt algorithm is not decryptable and to match the hashed string with plain string we use Hash::check method. For hashing the password laravel use secure Bcrypt and Argon2 hashing for storing user passwords. So in this tutorial i will show you to check password with hashed…

Read More
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 Laravel hasMany

How to Use hasMany Relationship in Laravel with Example ?

February 27, 2022November 10, 2023

Laravel hasMany relationship is used to create the relation between two tables. hasMany means create the relation one to Many. For example if a article have comments and we wanted to get all comments of the article then we can use hasMany relationship . Database Relations are used to create…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version