Magento2 : How to deal with zip files

Alternativaly you can use the Magento framework class of M2 called Magento\Framework\Archive\Zip and call the pack() method.

For example, if you injected the class and assigned it to the $zipArchive variable you can do:

$this->zipArchive->pack($source, $destination);

You can do this the same way

$zip = new \ZipArchive();

if ($zip->open($Zippath) === TRUE) {
    $zip->addFile($Filepath, $Filename);
    $zip->addFile($Filepath, "toto.txt");
    $zip->close();
    return TRUE; 
}

Its basically a php class. It has nothing to do with Magento. However you can check the implementation is still like the same.

Here is the class http://php.net/manual/en/class.ziparchive.php

And here is the implementation in Magento2.

If you open

lib\internal\Magento\Framework\Archive\Zip.php you will find this

public function pack($source, $destination)
    {
        $zip = new \ZipArchive();
        $zip->open($destination, \ZipArchive::CREATE);
        $zip->addFile($source);
        $zip->close();
        return $destination;
    }

Tags:

Zip

Magento2