Compress the contents of a directory to a ZIP archive using the PHP functions.

@param string $sourceDirectory
@param string $zipFile
@param array $options

@throws Exception
340791
protected function zipPHP($sourceDirectory, $zipFile, array $options)
    {
        if (!class_exists('ZipArchive')) {
            throw new Exception('Unable to zip files using ZipArchive. Please ensure the Zip extension is installed.');
        }
        $zip = new ZipArchive();
        try {
            $flags = ZipArchive::CREATE;
            if (!$options['skipCheck']) {
                $flags |= ZipArchive::CHECKCONS;
            }
            $zipErr = @$zip->open($zipFile, $flags);
            if ($zipErr !== true) {
                throw new Exception($this->describeZipArchiveError($zip, $zipErr));
            }
            $skipPathLength = strlen(rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $sourceDirectory), '/')) + 1;
            $contents = new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($sourceDirectory),
                \RecursiveIteratorIterator::SELF_FIRST
            );
            foreach ($contents as $item) {
                switch ($item->getFilename()) {
                    case '.':
                    case '..':
                        break;
                    default:
                        $itemFullPath = $item->getRealPath();
                        $itemRelPath = substr(str_replace(DIRECTORY_SEPARATOR, '/', $itemFullPath), $skipPathLength);
                        if (
                            $options['includeDotFiles']
                            ||
                            (
                                strpos($itemRelPath, '.') !== 0
                                &&
                                strpos($itemRelPath, '/.') === false
                            )
                        ) {
                            if ($item->isDir()) {
                                $added = @$zip->addEmptyDir($itemRelPath);
                            } else {
                                $added = @$zip->addFile($itemFullPath, $itemRelPath);
                            }
                            if ($added !== true) {
                                throw new Exception($this->describeZipArchiveError($zip, ZipArchive::ER_OK));
                            }
                        }
                        break;
                }
            }
            if (@$zip->close() !== true) {
                throw new Exception($this->describeZipArchiveError($zip, ZipArchive::ER_OK));
            }
            $zip = null;
        } catch (Exception $x) {
            if ($zip !== null) {
                try {
                    @$zip->close();
                } catch (\Exception $foo) {
                }
                $zip = null;
            }
            @$this->getFilesystem()->delete([$zipFile]);
            throw $x;
        }
    }
1004666
public static void zip(File[] sources, File target, String compressFileName) throws IOException {
		$.notEmpty(sources);
		$.notNull(target);
		if (!target.isDirectory()) {
			throw new IllegalArgumentException("The target can be a directory");
		}
		// 压缩后文件的名称
		compressFileName = $.notEmpty(compressFileName) ? compressFileName : sources[0].getName() + ".zip";
		FileOutputStream fileOut = new FileOutputStream(target.getAbsolutePath() + File.separator + compressFileName);
		ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(fileOut);
		for (int i = 0; i < sources.length; i++) {
			compress(sources[i], zipOut, null);
		}
		zipOut.close();
	}
1220071
protected function unzipNative($zipFile, $destinationDirectory, array $options)
    {
        $cmd = 'unzip';
        $cmd .= ' -o'; // overwrite files WITHOUT prompting
        $cmd .= ' -q'; // quiet mode, to avoid overflow of stdout
        $cmd .= ' ' . escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $zipFile)); // file to extract
        $cmd .= ' -d ' . escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $destinationDirectory)); // destination directory
        $rc = 1;
        $output = [];
        @exec($cmd . ' 2>&1', $output, $rc);
        if ($rc !== 0) {
            $error = trim(implode("\n", $output)) ?: t('Unknown error decompressing a ZIP archive');
            throw new Exception($error);
        }
    }
90643
public static function folderToZip($folder, &$zipFile, $exclusiveLength, $exclude = array('.git'), $prefix = '') {
	if (!file_exists($folder)) return;
    $handle = opendir($folder);
    while (false !== $f = readdir($handle)) {
      if ($f != '.' && $f != '..') {
		  
		if (in_array($f, $exclude)) continue;
		  
        $filePath = "$folder/$f";
        // Remove prefix from file path before add to zip.
        $localPath = $prefix.substr($filePath, $exclusiveLength);
        if (is_file($filePath)) {
          $zipFile->addFile($filePath, $localPath);
        } elseif (is_dir($filePath)) {
          // Add sub-directory.
          $zipFile->addEmptyDir($localPath);
          self::folderToZip($filePath, $zipFile, $exclusiveLength, $exclude, $prefix);
        }
      }
    }
    closedir($handle);
  }
484449
public static void createZipFile(Logger log, File sourceDir, File outputZipFile) throws IOException {
        FileFilter filter = null;
        createZipFile(log, sourceDir, outputZipFile, filter);
    }
707864
protected function extract($zipFile, $directory, OutputInterface $output = null)
    {
        if ($output) {
            $output->writeln('<info>Extracting file...</info>');
        }

        $archive = new ZipArchive;
        $archive->open($zipFile);
        $archive->extractTo($directory);
        $archive->close();

        return $this;
    }
91360
protected function extract($zipFile, $directory)
    {
        $archive = new ZipArchive;

        $archive->open($zipFile);

        $archive->extractTo($directory);

        $archive->close();

        return $this;
    }
1003056
public static void unZipFiles(File zipFile, File outputFolder) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(zipFile));
        try {
            unZipFiles(is, outputFolder);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
671784
public static void zip(File[] sources, File target) throws IOException {
		zip(sources, target, null);
	}
1237422
private static void compressDirectory(String relativePath, File srcPath, ZipOutputStream zip) throws IOException
   {
      if (srcPath.isDirectory())
      {
         zip.putNextEntry(new ZipEntry(relativePath + "/" + srcPath.getName() + "/"));
         zip.closeEntry();

         String files[] = srcPath.list();
         for (int i = 0; i < files.length; i++)
         {
            compressDirectory(relativePath + "/" + srcPath.getName(), new File(srcPath, files[i]), zip);
         }
      }
      else
      {
         InputStream in = new FileInputStream(srcPath);
         try
         {
            zip.putNextEntry(new ZipEntry(relativePath + "/" + srcPath.getName()));

            transfer(in, zip);

            zip.closeEntry();
         }
         finally
         {
            if (in != null)
            {
               in.close();
            }
         }
      }
   }
1278787
private void zipDirectory(String zipFile, String dirToZip) {
    File directory = new File(dirToZip);
    try (ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(zipFile))) {
      addDirectoryToZip(directory, directory, stream);
    } catch (Exception e) {
      throw new PlatformManagerException("Failed to zip module", e);
    }
  }
538277
public static function zip($i, $o, $del = false, $afilename = null, $s = 0){
        $i     = str_replace('\\', '/', $i);
        $zp = new \ZipArchive();
        
        if(file_exists($o)){
            $flags = 0;
        }else{
            $flags = \ZipArchive::CREATE;
        }
        
        $zp->open($o, $flags);
        
		$afilename = (is_string($afilename)) ? $afilename : basename($i);
        $success = $zp->addFile($i, $afilename);
        
        $zp->close();
        
        if(true===$del)unlink($afilename);
         
        if($success){
            if($s > 0){
                return self::split($o, $s);
            }
        }else{
            return false;
        }
        
       
    }
428921
public function getValidPortableElementFromZipSource($type, $zipFile)
    {
        /** @var PortableElementPackageParser $parser */
        $parser = $this->getPortableModelRegistry()->getModel($type)->getPackageParser();
        $source = $parser->extract($zipFile);
        $object = $parser->getModel()->createDataObject($parser->getManifestContent($zipFile));
        $this->validate($object, $source);

        return $object;
    }
1273841
private function fillZipWithFileContents($zipFileUrl)
    {
        $this->zip->open($zipFileUrl, \ZipArchive::CREATE);
        $this->finalizeSheet();
        $this->finalizeStyles();
        $this->finalizeDefaultXmls();
    }
1328171
public static File compressFileGzip(String _sourceFile, String _outputFileName) {
        return compress(CompressionMethod.GZIP, _sourceFile, _outputFileName);
    }
1340362
protected function extractZip($srcPath, $extractToDir)
    {
        $zip = new ZipArchive();
        $r = $zip->open($srcPath);
        if($r) {
            $zip->extractTo($extractToDir);
            $zip->close();             
        }        
    }
653391
public static void recursivelyCopyDirectory(final File sourceDir, final File destDir,
      final StandardCopyOption copyOption)
      throws IOException {
    checkNotNull(sourceDir);
    checkNotNull(destDir);
    checkArgument(sourceDir.isDirectory(), "Source directory does not exist");
    java.nio.file.Files.createDirectories(destDir.toPath());
    walkFileTree(sourceDir.toPath(), new CopyFileVisitor(sourceDir.toPath(), destDir.toPath(),
        copyOption));
  }