class Zip

Defines a archiver implementation for .zip files.

@link http://php.net/zip

Hierarchy

Expanded class hierarchy of Zip

File

drupal/core/lib/Drupal/Component/Archiver/Zip.php, line 17
Definition of Drupal\Component\Archiver\Zip.

Namespace

Drupal\Component\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\Component\Archiver\ArchiverException
   */
  public function __construct($file_path) {
    $this->zip = new ZipArchive();
    if ($this->zip
      ->open($file_path) !== TRUE) {
      throw new ArchiverException(t('Cannot open %file_path', array(
        '%file_path' => $file_path,
      )));
    }
  }

  /**
   * Implements Drupal\Component\Archiver\ArchiveInterface::add().
   */
  public function add($file_path) {
    $this->zip
      ->addFile($file_path);
    return $this;
  }

  /**
   * Implements Drupal\Component\Archiver\ArchiveInterface::remove().
   */
  public function remove($file_path) {
    $this->zip
      ->deleteName($file_path);
    return $this;
  }

  /**
   * Implements Drupal\Component\Archiver\ArchiveInterface::extract().
   */
  public function extract($path, array $files = array()) {
    if ($files) {
      $this->zip
        ->extractTo($path, $files);
    }
    else {
      $this->zip
        ->extractTo($path);
    }
    return $this;
  }

  /**
   * Implements Drupal\Component\Archiver\ArchiveInterface::listContents().
   */
  public function listContents() {
    $files = array();
    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

Namesort descending Modifiers Type Description Overrides
Zip::$zip protected property The underlying ZipArchive instance that does the heavy lifting.
Zip::add public function Implements Drupal\Component\Archiver\ArchiveInterface::add(). Overrides ArchiverInterface::add
Zip::extract public function Implements Drupal\Component\Archiver\ArchiveInterface::extract(). Overrides ArchiverInterface::extract
Zip::getArchive public function Retrieves the zip engine itself.
Zip::listContents public function Implements Drupal\Component\Archiver\ArchiveInterface::listContents(). Overrides ArchiverInterface::listContents
Zip::remove public function Implements Drupal\Component\Archiver\ArchiveInterface::remove(). Overrides ArchiverInterface::remove
Zip::__construct public function Constructs a Zip object.