Create a ZIP compressed file from an array of file data.

@param   string  $archive  Path to save archive.
@param   array   $files    Array of files to add to archive.

@return  boolean  True if successful.

@since   1.0
@todo    Finish Implementation
182103
public function create($archive, $files)
	{
		$contents = array();
		$ctrldir  = array();

		foreach ($files as $file)
		{
			$this->addToZipFile($file, $contents, $ctrldir);
		}

		return $this->createZipFile($contents, $ctrldir, $archive);
	}
1101299
public function decompress($content)
    {
        $archive = $this->getArchive();

        //check if there are null byte characters before doing a file_exists check
        if (!strstr($content, "\0") && file_exists($content)) {
            $archive = $content;
        }

        if (file_exists($archive)) {
            $file = bzopen($archive, 'r');
            if (!$file) {
                throw new Exception\RuntimeException("Error opening the archive '" . $content . "'");
            }

            $compressed = bzread($file);
            bzclose($file);
        } else {
            $compressed = bzdecompress($content);
        }

        if (is_int($compressed)) {
            throw new Exception\RuntimeException('Error during decompression');
        }

        return $compressed;
    }
1312485
protected static function expandFileList($archiveFiles, $files)
    {
        $newFiles = [];
        foreach ($files as $file) {
            foreach ($archiveFiles as $archiveFile) {
                if (fnmatch($file.'*', $archiveFile))
                    $newFiles[] = $archiveFile;
            }
        }
        return $newFiles;
    }
524957
public function compress($source, $target = '')
    {
        $target = $target ?: $source.'.zip';

        if ($this->zip->open($target, \ZipArchive::OVERWRITE | \ZipArchive::CREATE) === false) {
            return false;
        }

        if ($this->zip->addFile($source, basename($source)) === false) {
            $this->zip->close();

            return false;
        }

        return $this->zip->close();
    }
819967
private function createArchiveFile($pathToZip)
    {
        if (!$this->file->exists($pathToZip)) {
            $dirname = dirname($pathToZip);
            if (!$this->file->exists($dirname) && !$this->file->makeDirectory($dirname, 0755, true)) {
                throw new \RuntimeException('Failed to create folder');
            } elseif (!$this->file->isWritable($dirname)) {
                throw new Exception(sprintf('The path "%s" is not writeable', $pathToZip));
            }

            return true;
        }

        return false;
    }
250391
public static function js_files(array $files) {
        if (empty($files)) {
            return '';
        }

        $compressed = array();
        foreach ($files as $file) {
            $content = file_get_contents($file);
            if ($content === false) {
                $compressed[] = "\n\n// Cannot read JS file ".basename(dirname(dirname($file))).'/'.basename(dirname($file)).'/'.basename($file)."\n\n";
                continue;
            }
            $compressed[] = self::js($content);
        }

        return implode(";\n", $compressed);
    }
408645
public function setFiles(array $files) {
        if(!$this->setModified('files', $files)->isModified()) {
            return $this;
        }
		$this->files = $files;
		return $this;
    }
1255815
public function setFiles($files = [])
    {
        if (!$files instanceof FileBag) {
            $files = new FileBag($files);
        }

        $this->_files = $files;
    }
341040
public function createZipFile($path = null)
    {
        if ($path === null) {
            $path = tempnam(sys_get_temp_dir(), 'temp-zip-');
        }
        $zip = new \ZipArchive();
        $zip->open($path, \ZipArchive::CREATE);
        $this->addYmlFiles($zip);
        $zip->close();

        return $path;
    }
821887
public function extract(\ZipArchive $archive): void
    {
        $destination = $this->pluginDir;

        if (!is_writable($destination)) {
            throw new PluginExtractionException(sprintf('Destination directory "%s" is not writable', $destination));
        }

        $pluginName = $this->getPluginName($archive);
        $this->validatePluginZip($pluginName, $archive);

        $oldFile = $this->findOldFile($pluginName);
        $backupFile = $this->createBackupFile($oldFile);

        try {
            $archive->extractTo($destination);

            if ($backupFile !== '') {
                $this->filesystem->remove($backupFile);
            }

            unlink($archive->filename);
        } catch (\Exception $e) {
            if ($backupFile !== '') {
                $this->filesystem->rename($backupFile, $oldFile);
            }
            throw $e;
        }

        $this->clearOpcodeCache();
    }
116424
public function zipInit($archive, $callback = null)
    {
        $tempPath = $this->getAssetDirectory().'/#tmp/'.uniqid();

        $result = $this->traceZip($archive, $tempPath);

        $this->files->deleteDirectory($tempPath);

        if (!is_null($callback))
            call_user_func($callback, $result[0], $result[1]);

        return $result[0];
    }
1373670
public function compress($content)
    {
        $zip = new ZipArchive();
        $res = $zip->open($this->getArchive(), ZipArchive::CREATE | ZipArchive::OVERWRITE);

        if ($res !== true) {
            throw new Exception\RuntimeException($this->errorString($res));
        }

        if (file_exists($content)) {
            $content  = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, realpath($content));
            $basename = substr($content, strrpos($content, DIRECTORY_SEPARATOR) + 1);
            if (is_dir($content)) {
                $index    = strrpos($content, DIRECTORY_SEPARATOR) + 1;
                $content .= DIRECTORY_SEPARATOR;
                $stack    = [$content];
                while (!empty($stack)) {
                    $current = array_pop($stack);
                    $files   = [];

                    $dir = dir($current);
                    while (false !== ($node = $dir->read())) {
                        if (($node == '.') || ($node == '..')) {
                            continue;
                        }

                        if (is_dir($current . $node)) {
                            array_push($stack, $current . $node . DIRECTORY_SEPARATOR);
                        }

                        if (is_file($current . $node)) {
                            $files[] = $node;
                        }
                    }

                    $local = substr($current, $index);
                    $zip->addEmptyDir(substr($local, 0, -1));

                    foreach ($files as $file) {
                        $zip->addFile($current . $file, $local . $file);
                        if ($res !== true) {
                            throw new Exception\RuntimeException($this->errorString($res));
                        }
                    }
                }
            } else {
                $res = $zip->addFile($content, $basename);
                if ($res !== true) {
                    throw new Exception\RuntimeException($this->errorString($res));
                }
            }
        } else {
            $file = $this->getTarget();
            if (!is_dir($file)) {
                $file = basename($file);
            } else {
                $file = "zip.tmp";
            }

            $res = $zip->addFromString($file, $content);
            if ($res !== true) {
                throw new Exception\RuntimeException($this->errorString($res));
            }
        }

        $zip->close();
        return $this->options['archive'];
    }
324697
public function install($archive, $name)
    {
        $exploadName = explode('.', $name);
        $name = $exploadName[0];
        $storageData = route()->storage_data;
        $file = $storageData.$archive;
        $files = new Files();
        $files->mkdir($storageData.'tmp/');
        $files->mkdir($storageData.'tmp/'.$name);
        (new Zip())->extract($file, $storageData.'tmp/'.$name.'/', true);
        if (file_exists($storageData.'tmp/'.$name.'/component.json')) {
            $file = new FileHandling();
            $c = $file->open($storageData.'/tmp/'.$name.'/component.json', 'readOnly')->read();
            $file->close();
            $config = json_decode($c, true);
            if ($this->isSupported($config['requires']['version'], $config['requires']['comparator']) === true) {
                if (!file_exists(route()->com.$name)) {
                    $files->moveDir($storageData.'tmp/', route()->com, $name);

                    return true;
                }
            }
        }

        return false;
    }
920332
public function unzip($file, $destinationPath)
    {
        if (!class_exists('\ZipArchive')) {
            throw new Error("Class ZipArchive does not installed. Cannot unzip the file.");
        }

        $zip = new \ZipArchive;
        $res = $zip->open($file);

        if ($res === TRUE) {

            $this->getFileManager()->mkdir($destinationPath);

            $zip->extractTo($destinationPath);
            $zip->close();

            return true;
        }

        return false;
    }
525870
public function createPackageZip()
	{
		$zip = new \ZipArchive($this->target, \ZipArchive::CREATE);

		// Instantiate the zip archive
		$zip->open($this->target, \ZipArchive::CREATE);

		// Process the files to zip
		$this->addFiles($zip, JPATH_BASE . '/dist/zips/');

		$pkg_path = $this->current . "/administrator/manifests/packages/pkg_" . $this->getExtensionName();

		$zip->addFile($pkg_path . ".xml", "pkg_" . $this->getExtensionName() . ".xml");
		$zip->addFile($this->current . "/administrator/manifests/packages/" . $this->getExtensionName() . "/script.php", "script.php");

		// If the package has language files, add those
		$pkg_languages_path = $pkg_path . "/language";
		$languages          = glob($pkg_languages_path . "/*/*.pkg_" . $this->getExtensionName() . "*.ini");

		// Add all package language files
		foreach ($languages as $lang_path)
		{
			$path_in_zip = substr($lang_path, strlen($pkg_path) + 1);
			$zip->addFile($lang_path, $path_in_zip);
		}

		// Close the zip archive
		$zip->close();
	}
713755
public static function unZip($zip_file, $destination)
    {
        $zip = new \ZipArchive();
        $archive = $zip->open($zip_file);

        if ($archive === true) {
            Folder::create($destination);

            $unzip = $zip->extractTo($destination);


            if (!$unzip) {
                self::$error = self::ZIP_EXTRACT_ERROR;
                Folder::delete($destination);
                $zip->close();
                return false;
            }

            $package_folder_name = preg_replace('#\./$#', '', $zip->getNameIndex(0));
            $zip->close();

            return $destination . '/' . $package_folder_name;
        }

        self::$error = self::ZIP_EXTRACT_ERROR;
        self::$error_zip = $archive;
        return false;
    }
385286
protected function flattenArchive(ZipFile $archive)
    {
        $validator = new validator\FileLocationValidator();
        foreach ($archive->getListFiles() as $filename) {
            if (\Stringy\StaticStringy::endsWith($filename, '/')) {
                $archive->deleteFromName($directory);
            } else {
                $basename = $validator->getBasename($filename);
                if ($filename !== $basename) {
                    if (in_array(strtolower($basename), array_map('strtolower', $archive->getListFiles()))) {
                        throw SyntaxException(_('アーカイブ中に同名のファイルを含めることはできません: ') . $basename);
                    }
                    $archive->rename($filename, $basename);
                }
            }
        }
    }