paolo@bimodesign.com | +34 608 61 64 10

Framework

        

Zend 2 - How to zip file

To zip one or more files crypting using a password, I use the Compress Zend 2 method in this way.

use Zend\Filter\Compress;

class AdminController extends AbstractActionController {

    private function _LoadOffers($file) {
	// Get name file and generate .zip

        $fileLogArray = explode("/",$file);
        $fileLog=end($fileLogArray);
	
        $fileLogEmailArray = explode(".",$fileLog);
        $fileZipEmail = $fileLogEmailArray[0]."-".date('dmY_His').".zip"; 
        
        $urlXls = getcwd()."/data/".$fileLog;
        $urlZip = getcwd()."/data/".$fileZipEmail;

	// Instance the Compress class        
        $filter = new Compress(array(
            'adapter' => 'Zip',
            'options' => array(
                'archive' => $urlZip,
                'password' => "vass"
            )
        ));

	// In case of only one file
        $compress = $filter->filter($urlXls);
        
	// In case of more than one file
	//
        //foreach($files2 as $file){
        //    $compress = $filter->filter($file);
        //}        
	...       
    }
}

The oficial documentation here