Services\Synchronize::Zip PHP Method

Zip() public method

Compresses a folder
public Zip ( [type] $source, [type] $destination, $include_dir = true )
$source [type]
$destination [type]
    public function Zip($source, $destination, $include_dir = true)
    {
        if (!extension_loaded('zip') || !file_exists($source)) {
            return false;
        }
        if (file_exists($destination)) {
            unlink($destination);
        }
        $zip = new \ZipArchive();
        if (!$zip->open($destination, \ZIPARCHIVE::CREATE)) {
            return false;
        }
        $source = str_replace('\\', '/', realpath($source));
        if (is_dir($source) === true) {
            $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
            if ($include_dir) {
                $arr = explode("/", $source);
                $maindir = $arr[count($arr) - 1];
                $source = "";
                for ($i = 0; $i < count($arr) - 1; $i++) {
                    $source .= '/' . $arr[$i];
                }
                $source = substr($source, 1);
                $zip->addEmptyDir($maindir);
            }
            foreach ($files as $file) {
                // Ignore "." and ".." folders
                if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) {
                    continue;
                }
                $file = realpath($file);
                $file = str_replace('\\', '/', $file);
                if (is_dir($file) === true) {
                    $dir = str_replace($source . '/', '', $file . '/');
                    $zip->addEmptyDir($dir);
                } else {
                    if (is_file($file) === true) {
                        $new_file = str_replace($source . '/', '', $file);
                        $zip->addFromString($new_file, file_get_contents($file));
                    }
                }
            }
        } else {
            if (is_file($source) === true) {
                $zip->addFromString(basename($source), file_get_contents($source));
            }
        }
        return $zip->close();
    }