Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

2 Effective Methods to Create A Zip File in Magento 2

Vinh Jacker | 06-21-2024

2 Effective Methods to Create A Zip File in Magento 2

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/.

Logo 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.

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support & Maintenance Services

    mageplaza services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.