![](/style/images/good.png)
![](/style/images/bad.png)
How to Create ZIP Archive with Files in Laravel
source link: https://www.laravelcode.com/post/how-to-create-zip-archive-with-files-in-laravel
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Create ZIP Archive with Files in Laravel
Hello, everyone in this article we will share with you how to create a zip file using zipArchive
class. In many laravel application, you need to create functionality to many of file's make in one zip file and then download it.
You can find many laravel packages which provide you zip create functionality and easy to use. but in this article we will share with you how to create zip file help of zipArchive
class. here no need to install any extra packages.
What is zipArchive ?
zipArchive is one of the php's class. help of this class and methods you can easy to work with zip related all tasks and functionality. like, create zip, add a file to zip, add the directory in zip, add more than one directory in zip, etc... for more information please visit this link zipArchive Class.
Step - 1 Create route
Now, we first need to create one route in routes/web.php
file.
<?php
use App\Http\Controllers\CreateZipController;
Route::get('create-zip', [CreateZipController::class, 'index'])->name('create-zip');
Step - 2 Create controller
After creating route then we need to create also CreateZipController
controller using the following command run in your terminal or cmd.
php artisan make:controller CreateZipController
After run this commant your CreateZipController.php
file automatic created on app/Http/Controllers
folder. just open it and write following code into that file.
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use ZipArchive;
class CreateZipController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if($request->has('download')) {
// Define Dir Folder
$public_dir=public_path();
// Zip File Name
$zipFileName = 'AllDocuments.zip';
// Create ZipArchive Obj
$zip = new ZipArchive;
if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
// Add File in ZipArchive
$zip->addFile(file_path,'file_name');
// Close ZipArchive
$zip->close();
}
// Set Header
$headers = array(
'Content-Type' => 'application/octet-stream',
);
$filetopath=$public_dir.'/'.$zipFileName;
// Create Download Response
if(file_exists($filetopath)){
return response()->download($filetopath,$zipFileName,$headers);
}
}
return view('createZip');
}
}
How to add more file in zip
if you want to add more then one files or multiple files in created zip then you can use following code snippet for adding more in in zipArchive
class.
if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
// Add Multiple file
foreach($files as $file) {
$zip->addFile($file->path, $file->name);
}
$zip->close();
}
Step - 3 Create blade file
Into the last, we need to create one blade file which we use simple HTML layout for creating a zip file. so, in this blade file, we simply add one create zip button HTML code. but you can do it depends on your own functionality.
<!DOCTYPE html>
<html>
<head>
<title>Create Zip</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<a href="{{ route('create-zip',['download'=>'zip']) }}" class="btn btn-info" >Download ZIP</a>
</div>
</body>
</html>
Conclusion
As you can see, create zip file in laravel application is very easy help of using zipArchive
class. Here we don't need to use any laravel packages for create zip file.
We are hope this tutorials help everyone. if you have any issues or question reagarding create zip using of zipArchive so please comment bellow. Thanks..
Author : Harsukh Makwana
![Harsukh Makwana](https://www.laravelcode.com/upload/author.jpg)
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK