Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ZipArchiver.php

class ZipArchiver

@author Jan Prieser <jan@prieser.net>

Hierarchy

  • class \Composer\Package\Archiver\ZipArchiver implements \Composer\Package\Archiver\ArchiverInterface

Expanded class hierarchy of ZipArchiver

File

vendor/composer/composer/src/Composer/Package/Archiver/ZipArchiver.php, line 21

Namespace

Composer\Package\Archiver
View source
class ZipArchiver implements ArchiverInterface {
    
    /** @var array<string, bool> */
    protected static $formats = [
        'zip' => true,
    ];
    
    /**
     * @inheritDoc
     */
    public function archive(string $sources, string $target, string $format, array $excludes = [], bool $ignoreFilters = false) : string {
        $fs = new Filesystem();
        $sourcesRealpath = realpath($sources);
        if (false !== $sourcesRealpath) {
            $sources = $sourcesRealpath;
        }
        unset($sourcesRealpath);
        $sources = $fs->normalizePath($sources);
        $zip = new ZipArchive();
        $res = $zip->open($target, ZipArchive::CREATE);
        if ($res === true) {
            $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters);
            foreach ($files as $file) {
                
                /** @var \Symfony\Component\Finder\SplFileInfo $file */
                $filepath = strtr($file->getPath() . "/" . $file->getFilename(), '\\', '/');
                $localname = $filepath;
                if (strpos($localname, $sources . '/') === 0) {
                    $localname = substr($localname, strlen($sources . '/'));
                }
                if ($file->isDir()) {
                    $zip->addEmptyDir($localname);
                }
                else {
                    $zip->addFile($filepath, $localname);
                }
                
                /**
                 * setExternalAttributesName() is only available with libzip 0.11.2 or above
                 */
                if (method_exists($zip, 'setExternalAttributesName')) {
                    $perms = fileperms($filepath);
                    
                    /**
                     * Ensure to preserve the permission umasks for the filepath in the archive.
                     */
                    $zip->setExternalAttributesName($localname, ZipArchive::OPSYS_UNIX, $perms << 16);
                }
            }
            if ($zip->close()) {
                return $target;
            }
        }
        $message = sprintf("Could not create archive '%s' from '%s': %s", $target, $sources, $zip->getStatusString());
        throw new \RuntimeException($message);
    }
    
    /**
     * @inheritDoc
     */
    public function supports(string $format, ?string $sourceType) : bool {
        return isset(static::$formats[$format]) && $this->compressionAvailable();
    }
    private function compressionAvailable() : bool {
        return class_exists('ZipArchive');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ZipArchiver::$formats protected static property @var array&lt;string, bool&gt;
ZipArchiver::archive public function @inheritDoc Overrides ArchiverInterface::archive
ZipArchiver::compressionAvailable private function
ZipArchiver::supports public function @inheritDoc Overrides ArchiverInterface::supports
RSS feed
Powered by Drupal