Create Zip File and Download in Laravel 9
The reaction to laravel 9 zip download is the main topic of this tutorial. This tutorial covers how to produce a zip file in Laravel 9 in depth. You’ve come to the right place if you want to see an example of how to produce a zip file and download it using Laravel 9. Laravel 9 create zip from folder, as seen.
Large volumes of data are frequently organized and compressed using the zip file format. It is a well-liked technique for lowering the size of huge files or folders for better sharing and storage. Zip files reduce the size of several files or folders while preserving their structure and contents by compressing them into a single archive. As a result, they are simpler to download or share online and take up less space when being transferred or stored.
In this article, I’ll demonstrate how to create a zip file in Laravel using the two methods below. one employing the stechstudio/laravel-zipstream package and the other ZipArchive. We’ll just make a folder called “myFiles” and a zip file from it.
Let’s examine each of the two situations separately.
public function __invoke()
{
$zip = new ZipArchive;
$fileName = 'myNewFile.zip';
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
{
$files = File::files(public_path('myFiles'));
foreach ($files as $key => $value) {
$relativeNameInZipFile = basename($value);
$zip->addFile($value, $relativeNameInZipFile);
}
$zip->close();
}
return response()->download(public_path($fileName));
}