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

Breadcrumb

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

function ZipArchiver::archive

@inheritDoc

Overrides ArchiverInterface::archive

File

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

Class

ZipArchiver
@author Jan Prieser <jan@prieser.net>

Namespace

Composer\Package\Archiver

Code

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);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal