class Zip

Same name in this branch
  1. 8.9.x core/modules/system/src/Plugin/Archiver/Zip.php \Drupal\system\Plugin\Archiver\Zip
Same name in other branches
  1. 9 core/modules/system/src/Plugin/Archiver/Zip.php \Drupal\system\Plugin\Archiver\Zip
  2. 9 core/lib/Drupal/Core/Archiver/Zip.php \Drupal\Core\Archiver\Zip
  3. 10 core/modules/system/src/Plugin/Archiver/Zip.php \Drupal\system\Plugin\Archiver\Zip
  4. 10 core/lib/Drupal/Core/Archiver/Zip.php \Drupal\Core\Archiver\Zip
  5. 11.x core/modules/system/src/Plugin/Archiver/Zip.php \Drupal\system\Plugin\Archiver\Zip
  6. 11.x core/lib/Drupal/Core/Archiver/Zip.php \Drupal\Core\Archiver\Zip

Defines a archiver implementation for .zip files.

@link http://php.net/zip

Hierarchy

Expanded class hierarchy of Zip

1 file declares its use of Zip
Zip.php in core/modules/system/src/Plugin/Archiver/Zip.php
1 string reference to 'Zip'
xa2.php in core/lib/Drupal/Component/Transliteration/data/xa2.php

File

core/lib/Drupal/Core/Archiver/Zip.php, line 10

Namespace

Drupal\Core\Archiver
View source
class Zip implements ArchiverInterface {
    
    /**
     * The underlying ZipArchive instance that does the heavy lifting.
     *
     * @var \ZipArchive
     */
    protected $zip;
    
    /**
     * Constructs a Zip object.
     *
     * @param string $file_path
     *   The full system path of the archive to manipulate. Only local files
     *   are supported. If the file does not yet exist, it will be created if
     *   appropriate.
     *
     * @throws \Drupal\Core\Archiver\ArchiverException
     */
    public function __construct($file_path) {
        $this->zip = new \ZipArchive();
        if ($this->zip
            ->open($file_path) !== TRUE) {
            throw new ArchiverException("Cannot open '{$file_path}'");
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function add($file_path) {
        $this->zip
            ->addFile($file_path);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function remove($file_path) {
        $this->zip
            ->deleteName($file_path);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function extract($path, array $files = []) {
        if ($files) {
            $this->zip
                ->extractTo($path, $files);
        }
        else {
            $this->zip
                ->extractTo($path);
        }
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function listContents() {
        $files = [];
        for ($i = 0; $i < $this->zip->numFiles; $i++) {
            $files[] = $this->zip
                ->getNameIndex($i);
        }
        return $files;
    }
    
    /**
     * Retrieves the zip engine itself.
     *
     * In some cases it may be necessary to directly access the underlying
     * ZipArchive object for implementation-specific logic. This is for advanced
     * use only as it is not shared by other implementations of ArchiveInterface.
     *
     * @return \ZipArchive
     *   The ZipArchive object used by this object.
     */
    public function getArchive() {
        return $this->zip;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Zip::$zip protected property The underlying ZipArchive instance that does the heavy lifting.
Zip::add public function Adds the specified file or directory to the archive. Overrides ArchiverInterface::add
Zip::extract public function Extracts multiple files in the archive to the specified path. Overrides ArchiverInterface::extract
Zip::getArchive public function Retrieves the zip engine itself.
Zip::listContents public function Lists all files in the archive. Overrides ArchiverInterface::listContents
Zip::remove public function Removes the specified file from the archive. Overrides ArchiverInterface::remove
Zip::__construct public function Constructs a Zip object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.