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:
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 ?