2 Effective Methods to Create A Zip File in Magento 2
Vinh Jacker | 3 days ago
The Most Popular Extension Builder for Magento 2
With a big catalog of 224+ extensions for your online store
Zip file is an effective method to manage and transfer multiple files and folders efficiently. In Magento 2, you can easily create zip files directly from the backend file manager. Whether you’re moving data or organizing your files, this process simplifies the task by compressing multiple files into a single, easy-to-handle package.
In this guide, we’ll walk through the step-by-step process of creating a zip file in Magento 2 via 2 methods. These methods are to create a function and directly use the Magento library function in a .phtml file. You’ll learn how to select the files, compress them, rename the zip file, and download it to your computer.
Let’s get started!
Why You Should Use Zip Files?
- File Size Reduction: Zip files allow you to group multiple files together, reducing their overall size. This makes it easier to share or transfer large sets of files via email or other means.
- Data Security: When you create a zip file, the data within it is encrypted. This ensures that your files remain secure during transmission.
- Efficient Handling: Instead of transferring individual files one by one, you can create a zip file containing all the necessary files. This simplifies the process for administrators and developers.
How to Create Zip File in Magento 2 | 2 Effective Ways
Method 1: Create the function
Running the below-mentioned code:
/**
* @return string
*/
public function getZip($source, $destination)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$zip = new ZipArchive();
$fileDriver = $objectManager->create(\Magento\Framework\Filesystem\Driver\File::class);
$fileSystemIo = $objectManager->create(\Magento\Framework\Filesystem\Io\File::class);
if ($fileDriver->isExists($destination)) {
$zip->open($destination, ZipArchive::OVERWRITE);
} else {
$zip->open($destination, ZipArchive::CREATE);
}
$fileInfo = $fileSystemIo->getPathInfo($source);
$zip->addFile($source, $fileInfo['basename']);
$zip->close();
return $destination;
}
You can call this function in your desired action. For example:
$source = '/var/www/html/zip/logo.png';
$destination = '/var/www/html/zip/logo.zip';
$block->getZip($source, $destination);
Method 2: Directly use the Magento library function in a .phtml file
Add the code below:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$zip = $objectManager->create(\Magento\Framework\Archive\Zip::class);
$source = '/var/www/html/zip/logo.png';
$destination = '/var/www/html/zip/logo.zip';
$zip->pack($source,$destination);
The result of both methods will be a zip file named logo.zip
created in the path /var/www/html/zip/
.
Conclusion
Creating zip files in Magento 2 streamlines file management enhances security, and simplifies data transfer. Whichever method you choose from the above two, you can successfully create a zip file. Feel free to share this solution with the Magento community and optimize your online store!
Hope you find this article helpful! If you have any doubts or need further assistance, feel free to ask.