Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Archiver;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Defines a archiver implementation for .zip files.
|
Chris@0
|
7 *
|
Chris@0
|
8 * @link http://php.net/zip
|
Chris@0
|
9 */
|
Chris@0
|
10 class Zip implements ArchiverInterface {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * The underlying ZipArchive instance that does the heavy lifting.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @var \ZipArchive
|
Chris@0
|
16 */
|
Chris@0
|
17 protected $zip;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Constructs a Zip object.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @param string $file_path
|
Chris@0
|
23 * The full system path of the archive to manipulate. Only local files
|
Chris@0
|
24 * are supported. If the file does not yet exist, it will be created if
|
Chris@0
|
25 * appropriate.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @throws \Drupal\Core\Archiver\ArchiverException
|
Chris@0
|
28 */
|
Chris@0
|
29 public function __construct($file_path) {
|
Chris@0
|
30 $this->zip = new \ZipArchive();
|
Chris@0
|
31 if ($this->zip->open($file_path) !== TRUE) {
|
Chris@0
|
32 throw new ArchiverException(t('Cannot open %file_path', ['%file_path' => $file_path]));
|
Chris@0
|
33 }
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 public function add($file_path) {
|
Chris@0
|
40 $this->zip->addFile($file_path);
|
Chris@0
|
41
|
Chris@0
|
42 return $this;
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * {@inheritdoc}
|
Chris@0
|
47 */
|
Chris@0
|
48 public function remove($file_path) {
|
Chris@0
|
49 $this->zip->deleteName($file_path);
|
Chris@0
|
50
|
Chris@0
|
51 return $this;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public function extract($path, array $files = []) {
|
Chris@0
|
58 if ($files) {
|
Chris@0
|
59 $this->zip->extractTo($path, $files);
|
Chris@0
|
60 }
|
Chris@0
|
61 else {
|
Chris@0
|
62 $this->zip->extractTo($path);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 return $this;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function listContents() {
|
Chris@0
|
72 $files = [];
|
Chris@0
|
73 for ($i = 0; $i < $this->zip->numFiles; $i++) {
|
Chris@0
|
74 $files[] = $this->zip->getNameIndex($i);
|
Chris@0
|
75 }
|
Chris@0
|
76 return $files;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Retrieves the zip engine itself.
|
Chris@0
|
81 *
|
Chris@0
|
82 * In some cases it may be necessary to directly access the underlying
|
Chris@0
|
83 * ZipArchive object for implementation-specific logic. This is for advanced
|
Chris@0
|
84 * use only as it is not shared by other implementations of ArchiveInterface.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return \ZipArchive
|
Chris@0
|
87 * The ZipArchive object used by this object.
|
Chris@0
|
88 */
|
Chris@0
|
89 public function getArchive() {
|
Chris@0
|
90 return $this->zip;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 }
|