3 namespace Drupal\Core\Archiver;
6 * Defines a archiver implementation for .zip files.
8 * @link http://php.net/zip
10 class Zip implements ArchiverInterface {
13 * The underlying ZipArchive instance that does the heavy lifting.
20 * Constructs a Zip object.
22 * @param string $file_path
23 * The full system path of the archive to manipulate. Only local files
24 * are supported. If the file does not yet exist, it will be created if
27 * @throws \Drupal\Core\Archiver\ArchiverException
29 public function __construct($file_path) {
30 $this->zip = new \ZipArchive();
31 if ($this->zip->open($file_path) !== TRUE) {
32 throw new ArchiverException(t('Cannot open %file_path', ['%file_path' => $file_path]));
39 public function add($file_path) {
40 $this->zip->addFile($file_path);
48 public function remove($file_path) {
49 $this->zip->deleteName($file_path);
57 public function extract($path, array $files = []) {
59 $this->zip->extractTo($path, $files);
62 $this->zip->extractTo($path);
71 public function listContents() {
73 for ($i = 0; $i < $this->zip->numFiles; $i++) {
74 $files[] = $this->zip->getNameIndex($i);
80 * Retrieves the zip engine itself.
82 * In some cases it may be necessary to directly access the underlying
83 * ZipArchive object for implementation-specific logic. This is for advanced
84 * use only as it is not shared by other implementations of ArchiveInterface.
87 * The ZipArchive object used by this object.
89 public function getArchive() {