Laravel 10 - Zip Files (Zip & Unzip files)
Laravel 10 - Zip Files (Zip & Unzip files)
In this tutorial, we will discuss how to download, zip, and unzip files. This is useful for managing file storage and transfer efficiently.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
In this step-by-step guide, we walk you through building a simple ZIP file creation and extraction feature in a Laravel application using the zanysoft/laravel-zip package. You’ll begin by setting up your Laravel project and installing frontend assets with Vite. Next, you'll integrate the ZIP package, generate a controller to handle compression logic, and create a route to trigger the functionality. The guide demonstrates how to zip a folder of images located in the public directory and serve it as a downloadable file. For additional functionality, you'll also learn how to extract the ZIP archive back into a folder using the same package. By the end, you’ll have a practical utility for managing ZIP files directly from your Laravel application, ideal for packaging and delivering groups of files to users.
Step # 1 : Initialize Your Laravel Application.
Begin by setting up your Laravel environment. You can start from scratch with a new project or use one you’ve already been working on. If you have Laravel installed globally, you can create a project by running.
laravel new zip
Or, if you prefer using Composer, run the following instead.
composer create-project laravel/laravel --prefer-dist zip
Both commands will scaffold a fully functional Laravel application, complete with essential components such as the .env file, routing files, and controllers. This provides a clean and ready-to-go foundation for development. Once your project is set up, you’ll have all the core Laravel files in place, allowing you to jump straight into building features without needing to worry about setting up basic configurations. This setup ensures that you can focus on the development logic right from the start.
Step # 2 : Open and Prepare Your Project.
Launch your terminal of choice (like Git Bash) and move into the root directory of your Laravel project. For example.
cd c:xampp/htdocs/zip
Once you're inside the project folder, set up the front-end tooling by installing dependencies and starting the Vite development server.
npm install && npm run dev
Keep this terminal open to let Vite handle your asset compilation. Then, open a separate terminal window or tab, navigate back to the same directory, and continue working with Laravel commands as needed.
Step # 3 : Add the Laravel ZIP Package.
To enable ZIP file handling in your Laravel project, you'll need to install a dedicated package. This package will allow you to create, read, and extract ZIP archives directly within your application. In your terminal, make sure you're inside the root of your Laravel project, then run.
composer require zanysoft/laravel-zip
This command will pull in the zanysoft/laravel-zip package along with its dependencies, giving you access to helpful ZIP utilities for managing compressed files within your Laravel application. Once installed, you'll be able to easily handle ZIP files in your project, streamlining the process of compression and extraction.
Step # 4 : Generate a Controller for ZIP Functionality.
Now that the package is installed, let’s create a controller to handle ZIP operations. To compress and download a folder of images. Start by creating a controller named ZipController using the Artisan command.
php artisan make:controller ZipController
Next, open the newly created ZipController.php located in app/Http/Controllers, and update it with the following code.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Zip;
class ZipController extends Controller
{
public function index()
{
// Make sure there's a folder named 'images' inside the public directory.
// Add a few sample images to that folder for testing.
// Create a ZIP archive from the 'images' folder
// The resulting file will be saved as 'Nature_images.zip' in the public directory
$zip = Zip::create('Nature_images.zip');
$zip->add(public_path('/images'));
$zip->close();
// Return the ZIP file as a downloadable response
return response()->download(public_path('Nature_images.zip'));
}
}
This setup enables users to download a zipped version of the /public/images folder by simply accessing the controller’s route. It’s a quick and efficient method for packaging multiple files into a single downloadable ZIP archive. This feature can be extended to include dynamic content, like user-generated files, or customized for specific use cases, such as generating ZIP files on-demand. Additionally, you can modify the controller to allow users to download multiple directories or handle large files by streaming them instead of forcing a download. This approach makes file distribution easier and more organized for both developers and users.
Step # 5 : Update the Default Route to Trigger ZIP Download.
To test the ZIP functionality through your browser, we’ll connect the root URL (/) to the index method in ZipController. This means whenever someone visits your Laravel app’s homepage, the images folder will be zipped and offered as a download. Open the routes/web.php file and update or add the following line.
Route::get('/', [App\Http\Controllers\ZipController::class, 'index']);
Now, visiting http://localhost (or your Laravel app's base URL) will execute the logic inside ZipController@index, creating and downloading a ZIP file containing everything inside the public/images folder.
Step # 6 : Run and Test the Application.
With everything set up, it's time to run your Laravel project and test the ZIP functionality. First, start the development server using the Artisan command.
php artisan serve
Once the server is running, open your browser and navigate to: http://127.0.0.1:8000. This will trigger the index method in ZipController, which creates and downloads a ZIP file containing the images stored in the public/images folder.
Optional: Extract the ZIP File Instead
If you'd like to test extracting the ZIP file instead of downloading it, update the index method in your controller like this.
public function index()
{
// This will extract the contents of 'Nature_images.zip' into a folder called 'extractedFiles' in the public directory
$zip = Zip::open('Nature_images.zip');
$zip->extract(public_path() . '/extractedFiles');
dd("done");
}
Now when you visit the same URL, Laravel will extract the ZIP file instead of offering it as a download. This is helpful for testing both compression and extraction features of the package.
Conclusion
By completing this guide, you've successfully added ZIP file support to your Laravel application using the zanysoft/laravel-zip package. You've learned how to compress a folder into a downloadable archive and even extract its contents programmatically. This setup is great for features like packaging user data, exporting reports, or managing file bundles. To dive deeper into what this package offers such as advanced file selection, password protection, or streaming ZIP downloads, be sure to check out the official documentation provided by ZanySoft for Laravel Zip.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments