Пример #1
0
 public static function copyDirToArchive($dir, $archive, $basename = null, $excludeDirs = null)
 {
     $dir = rtrim($dir, '/\\');
     $basename = $basename ?: basename($dir);
     rex_dir::create(dirname($archive));
     $files = array();
     $iterator = rex_finder::factory($dir)->recursive()->filesOnly();
     if ($excludeDirs) {
         $iterator->ignoreDirs($excludeDirs, false);
     }
     foreach ($iterator as $path => $file) {
         $subpath = str_replace($dir, $basename, $path);
         $subpath = str_replace('\\', '/', $subpath);
         $files[$subpath] = $path;
     }
     if (class_exists('ZipArchive')) {
         $zip = new ZipArchive();
         $zip->open($archive, ZipArchive::CREATE);
         foreach ($files as $path => $realpath) {
             $zip->addFile($realpath, $path);
         }
         $zip->close();
     } else {
         $phar = new PharData($archive, 0, null, Phar::ZIP);
         $phar->buildFromIterator(new ArrayIterator($files));
         $phar->compressFiles(Phar::GZ);
         foreach ($files as $path => $realpath) {
             if (filesize($realpath) == 0) {
                 $phar[$path]->decompress();
             }
         }
     }
 }
Пример #2
0
 public function zip($from, $to)
 {
     if (!is_dir($from) || !is_readable($from)) {
         throw new Exception('Extension dir doesn\'t not exist or is not a readable directory');
     }
     $outputDir = dirname($to);
     if (!is_dir($outputDir) || !is_writable($outputDir)) {
         throw new Exception('Output dir doesn\'t not exist or is not a writable directory');
     }
     $phar = new PharData($to, null, null, PHAR::ZIP);
     $phar->buildFromDirectory($from);
     $phar->compressFiles(PHAR::GZ);
     unset($phar);
     if (!file_exists($to)) {
         throw new Exception('Can\'t create zip file');
     }
     if (PHP_SAPI == 'cli') {
         echo "Zip file is created\n";
     }
 }
Пример #3
0
/**
 * Compress files with Tar archiver
 *
 * @param string $archive_name - archive name (zip, tgz, gz and tar.gz supported)
 * @param string $file_list - list of files to place into archive
 * @param string $dirname - directory, where the files should be get from
 * @return bool true
 */
function fn_compress_files($archive_name, $file_list, $dirname = '')
{
    if (!class_exists('PharData')) {
        fn_set_notification('E', __('error'), __('error_class_phar_data_not_found'));
        return false;
    }
    if (empty($dirname)) {
        $dirname = Registry::get('config.dir.files');
    }
    if (!is_array($file_list)) {
        $file_list = array($file_list);
    }
    $ext = fn_get_file_ext($archive_name);
    $_exts = explode('.', $archive_name);
    array_shift($_exts);
    $first_dot_ext = '.' . implode('.', $_exts);
    // https://bugs.php.net/bug.php?id=58852. Phar gets ext from the first dot: 'test.1.2.3.tgz' -> ext = 1.2.3.tgz
    $arch = fn_normalize_path($dirname . '/' . $archive_name);
    fn_rm($arch);
    if ($ext != 'zip') {
        $arch = fn_normalize_path($dirname . '/' . $archive_name . '.tmp');
        fn_rm($arch);
    }
    if ($ext == 'gz' && strpos($archive_name, '.tar.gz') !== false) {
        $ext = 'tar.gz';
    }
    try {
        $phar = new PharData($arch);
        foreach ($file_list as $file) {
            $path = fn_normalize_path($dirname . '/' . $file);
            if (is_file($path)) {
                $phar->addFile($path, basename($path));
            } elseif (is_dir($path)) {
                $phar->buildFromDirectory($path);
            }
        }
        if ($ext == 'zip') {
            $phar->compressFiles(Phar::GZ);
        } else {
            $phar->compress(Phar::GZ, $first_dot_ext);
            // We need to unset Phar because the PharData class still has the file "open".
            // Windows servers cannot delete the files with the "open" handlers.
            unset($phar);
            fn_rm($arch);
        }
    } catch (Exception $e) {
        fn_set_notification('E', __('error'), $e->getMessage());
        return false;
    }
    return true;
}
 /**
  * @throws BuildException
  */
 public function main()
 {
     $this->checkPreconditions();
     try {
         $this->log('Building archive: ' . $this->destinationFile->__toString(), Project::MSG_INFO);
         /**
          * Delete old archive, if exists.
          */
         if ($this->destinationFile->exists()) {
             $isDeleted = $this->destinationFile->delete();
             if (!$isDeleted) {
                 $this->log("Could not delete destination file {$this->destinationFile}", Project::MSG_WARN);
             }
         }
         $pharData = new PharData($this->baseDirectory->getPath() . '/' . $this->destinationFile->getName());
         foreach ($this->filesets as $fileset) {
             $this->log('Adding specified files in ' . $fileset->getDir($this->project) . ' to archive', Project::MSG_VERBOSE);
             $pharData->buildFromIterator($fileset->getIterator(), $fileset->getDir($this->project));
         }
         if ($this->compression !== PHAR::NONE && $pharData->canCompress($this->compression)) {
             try {
                 $pharData->compress($this->compression);
             } catch (UnexpectedValueException $uve) {
                 $pharData->compressFiles($this->compression);
             }
             unset($pharData);
         }
     } catch (Exception $e) {
         throw new BuildException('Problem creating archive: ' . $e->getMessage(), $e, $this->getLocation());
     }
 }
Пример #5
0
 /**
  * @inheritDoc
  */
 public function close()
 {
     $this->phar->compressFiles(\Phar::GZ);
     $this->phar = null;
 }