public static function zip($source, $destination) {

        // Check if the filename exists.
        if (false === file_exists($source)) {
            throw new FileNotFoundException($source);
        }

        // Initialize the ZIP archive.
        $zip = new ZipArchive();
        $zip->open($destination, ZipArchive::CREATE);

        // Clean up.
        $src = str_replace("\\\\", "/", realpath($source));

        // Is file ? => Add it and return.
        if (true === is_file($src)) {
            $zip->addFromString(basename($src), static::getContents($src));
            return $zip->close();
        }

        // Handle the files list.
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($src), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $current) {

            // Clean up.
            $cur = str_replace("\\\\", "/", realpath($current));

            // Initialize the ZIP path.
            $zipPath = preg_replace("/^" . str_replace("/", "\/", $src . "/") . "/", "", $cur);

            // Check the file type.
            if (true === is_file($cur)) {
                $zip->addFromString($zipPath, static::getContents($cur));
            }
            if (true === is_dir($cur)) {
                $zip->addEmptyDir($zipPath);
            }
        }
    }
public static function zip($source, $destination) {

        // Check if the filename exists.
        if (false === file_exists($source)) {
            throw new FileNotFoundException($source);
        }

        // Initialize the ZIP archive.
        $zip = new ZipArchive();
        $zip->open($destination, ZipArchive::CREATE);

        // Clean up.
        $src = str_replace("\\\\", "/", realpath($source));

        // Is file ? => Add it and return.
        if (true === is_file($src)) {
            $zip->addFromString(basename($src), static::getContents($src));
            return $zip->close();
        }

        // Handle the files list.
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($src), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $current) {

            // Clean up.
            $cur = str_replace("\\\\", "/", realpath($current));

            // Initialize the ZIP path.
            $zipPath = preg_replace("/^" . str_replace("/", "\/", $src . "/") . "/", "", $cur);

            // Check the file type.
            if (true === is_file($cur)) {
                $zip->addFromString($zipPath, static::getContents($cur));
            }
            if (true === is_dir($cur)) {
                $zip->addEmptyDir($zipPath);
            }
        }
    }
public static function zip ( $ source , $ destination ) { // Check if the filename exists. if ( false === file_exists ( $ source ) ) { throw new FileNotFoundException ( $ source ) ; } // Initialize the ZIP archive. $ zip = new ZipArchive ( ) ; $ zip -> open ( $ destination , ZipArchive :: CREATE ) ; // Clean up. $ src = str_replace ( "\\\\" , "/" , realpath ( $ source ) ) ; // Is file ? => Add it and return. if ( true === is_file ( $ src ) ) { $ zip -> addFromString ( basename ( $ src ) , static :: getContents ( $ src ) ) ; return $ zip -> close ( ) ; } // Handle the files list. $ files = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $ src ) , RecursiveIteratorIterator :: SELF_FIRST ) ; foreach ( $ files as $ current ) { // Clean up. $ cur = str_replace ( "\\\\" , "/" , realpath ( $ current ) ) ; // Initialize the ZIP path. $ zipPath = preg_replace ( "/^" . str_replace ( "/" , "\/" , $ src . "/" ) . "/" , "" , $ cur ) ; // Check the file type. if ( true === is_file ( $ cur ) ) { $ zip -> addFromString ( $ zipPath , static :: getContents ( $ cur ) ) ; } if ( true === is_dir ( $ cur ) ) { $ zip -> addEmptyDir ( $ zipPath ) ; } } }
Zip a file.

@param string $source The source filename.
@param string $destination The destination filename.
@return bool Returns true in case of success, false otherwise.
@throws FileNotFoundException Throws a file not found exception if the source filename is not found.
Zip a file .