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 Unique Validation in laravel 8

Unique validation in Laravel with example

November 21, 2021June 17, 2022

Laravel also support validation for database operations. Laravel unique validation validates the table column uniqueness using unique validation. In this tutorial we will learn about the unique validation to table columns and also while updating the value too. you can use this to check the uniqueness of email, username ,…

Read More
Php Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

July 5, 2024July 5, 2024

In this article, we’ll delve into Laravel and explore the implementation of CRUD operations alongside essential features like Search, Image uploading, and Pagination. CRUD, an acronym for Create, Read, Update, and Delete, forms the backbone of database management in web applications across all programming paradigms. This tutorial covers these fundamental…

Read More
Php Simplest Way to Use Roles and Permission in Laravel

Simplest Way to Use Roles and Permission in Laravel

August 23, 2022March 16, 2024

Roles and permission in laravel are used to provide roles based authentication and in this post we will learn simplest wat to use roles and permission in laravel. In our application we want features and modules on basis of roles or permissions so that we can restrict users to access…

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