I want to create and download zip of a folder in my custom module. I followed this link, but getting fatal error Uncaught Error: Class 'Vendor\Module\Controller\Custom\ZipArchive' not found.

How to accomplish this. please help.

  • Please update your code, so that we can able to identify the error. It seems the file not there or may be wrong namespace. Can you please check it. Feb 14, 2019 at 10:08
  • It's look like class path is not correct. Can you please try to put some text in your function and then write die; check this text display or not.
    – user55548
    Feb 14, 2019 at 10:09
  • My controller is working but when i introduce $zip = new ZipArchive; this shows fatal error. Can you please see this link. Feb 14, 2019 at 10:16
  • new ZipArchive. It means ZipArchive is php function not a magento function. Can you please check ziparchive php extension is installed or not.
    – user55548
    Feb 14, 2019 at 10:27
  • try withthis new \ZipArchive()
    – kunj
    Feb 14, 2019 at 10:35

2 Answers 2


You can create a model file in your module.

File: MageExt/Download/Model/CustomDownload.php


namespace MageExt\Download\Model;

class CustomDownload
    protected $directoryList;
    protected $driver;
    protected $fileFactory;

    public function __construct(
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
        \Magento\Framework\Shell\Driver $driver,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory
        $this->directoryList = $directoryList;
        $this->driver = $driver;
        $this->fileFactory = $fileFactory;

    public function downloadCustomDocs()
        if (!class_exists('\ZipArchive')) {
            die('ZipArchive class not found');

        $dir = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::ROOT);
        $rootPath = $dir.'/app/code/CustomDocs';
        //$this->driver->execute('zip -r ../CustomDocs.zip *', []);
        $zip = new \ZipArchive();
        $zip->open('CustomDocs.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE);

        $files = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($rootPath),

        foreach ($files as $name => $file)
            // Skip directories (they would be added automatically)
            if (!$file->isDir())
                // Get real and relative path for current file
                $filePath = $file->getRealPath();
                $relativePath = substr($filePath, strlen($rootPath) + 1);

                // Add current file to archive
                $zip->addFile($filePath, $relativePath);


                    'type' => 'filename',
                    'value' => $dir.'/app/code/CustomDocs/CustomDocs.zip',
                    'rm' => true

Now you can call downloadCustomDocs() function from anywhere in your module.

  • can you please explain it by adding comments. i am getting cofused. Feb 14, 2019 at 12:18
  • Tell me the Vendor name, Module name and folder to zip inside your module ..
    – Yash Shah
    Feb 14, 2019 at 12:23
  • Vendor: MageExt, Module: Download. But the folder needs to download is in app/code directory, which is parallel to the module directory. The name of directory is "CustomDocs", Feb 14, 2019 at 12:25
  • Let me know the folder path including name of the folder which you want to download.
    – Yash Shah
    Feb 14, 2019 at 12:26
  • Folder path : app/code/CustomDocs which need to download. Feb 14, 2019 at 12:28

Whenever there is any structural (class added or class name/reference modified) in your modeule: Or Any JS/Less file contents change in your theme: You need to build your static content again and setup compile again as well. (There is a set of commands for this already)

Otherwise your changes will not be picked..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.