comparison core/lib/Drupal/Core/Archiver/Zip.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Core\Archiver;
4
5 /**
6 * Defines a archiver implementation for .zip files.
7 *
8 * @link http://php.net/zip
9 */
10 class Zip implements ArchiverInterface {
11
12 /**
13 * The underlying ZipArchive instance that does the heavy lifting.
14 *
15 * @var \ZipArchive
16 */
17 protected $zip;
18
19 /**
20 * Constructs a Zip object.
21 *
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
25 * appropriate.
26 *
27 * @throws \Drupal\Core\Archiver\ArchiverException
28 */
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]));
33 }
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public function add($file_path) {
40 $this->zip->addFile($file_path);
41
42 return $this;
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function remove($file_path) {
49 $this->zip->deleteName($file_path);
50
51 return $this;
52 }
53
54 /**
55 * {@inheritdoc}
56 */
57 public function extract($path, array $files = []) {
58 if ($files) {
59 $this->zip->extractTo($path, $files);
60 }
61 else {
62 $this->zip->extractTo($path);
63 }
64
65 return $this;
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function listContents() {
72 $files = [];
73 for ($i = 0; $i < $this->zip->numFiles; $i++) {
74 $files[] = $this->zip->getNameIndex($i);
75 }
76 return $files;
77 }
78
79 /**
80 * Retrieves the zip engine itself.
81 *
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.
85 *
86 * @return \ZipArchive
87 * The ZipArchive object used by this object.
88 */
89 public function getArchive() {
90 return $this->zip;
91 }
92
93 }