o2system /
filesystem
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Filesystem\Files; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Filesystem\Files\Abstracts\AbstractFile; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Class ZipFile |
||||
| 22 | * |
||||
| 23 | * @package O2System\Filesystem\Files |
||||
| 24 | */ |
||||
| 25 | class ZipFile extends AbstractFile |
||||
| 26 | {
|
||||
| 27 | /** |
||||
| 28 | * XmlFile::readFile |
||||
| 29 | * |
||||
| 30 | * @param string $filePath Path to the file. |
||||
| 31 | * @param array $options Read file options. |
||||
| 32 | * |
||||
| 33 | * @return mixed |
||||
| 34 | */ |
||||
| 35 | public function readFile($filePath = null, array $options = []) |
||||
| 36 | {
|
||||
| 37 | $filePath = empty($filePath) |
||||
| 38 | ? $this->filePath |
||||
| 39 | : $filePath; |
||||
| 40 | |||||
| 41 | |||||
| 42 | $result = []; |
||||
| 43 | |||||
| 44 | if (extension_loaded('zip')) {
|
||||
| 45 | if ($zipHandle = zip_open($filePath)) {
|
||||
| 46 | while ($zipContent = zip_read($zipHandle)) {
|
||||
| 47 | $result[] = zip_entry_name($zipContent); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | zip_close($zipHandle); |
||||
| 51 | } |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | return $result; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | // ------------------------------------------------------------------------ |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * ZipFile::writeFile |
||||
| 61 | * |
||||
| 62 | * @param string $filePath Path to the file. |
||||
| 63 | * @param array $options Write file options. |
||||
| 64 | * |
||||
| 65 | * @return bool Returns TRUE on success or FALSE on failure. |
||||
| 66 | */ |
||||
| 67 | public function writeFile($filePath = null, array $options = []) |
||||
| 68 | {
|
||||
| 69 | $filePath = empty($filePath) |
||||
| 70 | ? $this->filePath |
||||
| 71 | : $filePath; |
||||
| 72 | |||||
| 73 | if ($this->count()) {
|
||||
| 74 | $zip = new \ZipArchive(); |
||||
| 75 | |||||
| 76 | if ($zip->open($filePath, \ZipArchive::CREATE)) {
|
||||
| 77 | foreach ($this->getArrayCopy() as $directory => $files) {
|
||||
| 78 | if (is_array($files)) {
|
||||
| 79 | $zip->addEmptyDir($directory); |
||||
| 80 | foreach ($files as $file) {
|
||||
| 81 | if (is_file($file)) {
|
||||
| 82 | $zip->addFile($file); |
||||
| 83 | } |
||||
| 84 | } |
||||
| 85 | } elseif (is_file($files)) {
|
||||
| 86 | $zip->addFile($files); |
||||
| 87 | } |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | return $zip->close(); |
||||
| 91 | } |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | return false; |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | // ------------------------------------------------------------------------ |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * ZipFile::compress |
||||
| 101 | * |
||||
| 102 | * Zipped a folder (include itself). |
||||
| 103 | * |
||||
| 104 | * @param string $sourcePath Path of directory to be zip. |
||||
| 105 | * @param string|null $filePath Path to the zip file. |
||||
| 106 | */ |
||||
| 107 | public function compress($sourcePath, $filePath = null) |
||||
| 108 | {
|
||||
| 109 | $filePath = empty($filePath) |
||||
| 110 | ? $this->filePath |
||||
| 111 | : $filePath; |
||||
| 112 | |||||
| 113 | $zip = new \ZipArchive(); |
||||
| 114 | $zip->open($filePath, \ZipArchive::CREATE); |
||||
| 115 | $zip->addEmptyDir(pathinfo($sourcePath, PATHINFO_BASENAME)); |
||||
| 116 | $this->recursiveCompress($sourcePath, strlen(dirname($sourcePath, 2) . DIRECTORY_SEPARATOR), $zip); |
||||
| 117 | $zip->close(); |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | // ------------------------------------------------------------------------ |
||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * ZipFile::recursiveCompress |
||||
| 124 | * |
||||
| 125 | * Add files and sub-directories in a folder to zip file. |
||||
| 126 | * |
||||
| 127 | * @param string $directory Path of directory to be zip. |
||||
| 128 | * @param int $exclusiveLength Number of text to be exclusive from the file path. |
||||
| 129 | * @param \ZipArchive $zipArchive Zip Archive instance. |
||||
| 130 | */ |
||||
| 131 | private function recursiveCompress($directory, $exclusiveLength, &$zipArchive) |
||||
| 132 | {
|
||||
| 133 | $handle = opendir($directory); |
||||
| 134 | |||||
| 135 | while (false !== ($file = readdir($handle))) {
|
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 136 | if ($file != '.' && $file != '..') {
|
||||
| 137 | $filePath = "$directory/$file"; |
||||
| 138 | // Remove prefix from file path before add to zip. |
||||
| 139 | $localPath = substr($filePath, $exclusiveLength); |
||||
| 140 | if (is_file($filePath)) {
|
||||
| 141 | $zipArchive->addFile($filePath, $localPath); |
||||
| 142 | } elseif (is_dir($filePath)) {
|
||||
| 143 | // Add sub-directory. |
||||
| 144 | $zipArchive->addEmptyDir($localPath); |
||||
| 145 | $this->recursiveCompress($filePath, $exclusiveLength, $zipArchive); |
||||
| 146 | } |
||||
| 147 | } |
||||
| 148 | } |
||||
| 149 | closedir($handle); |
||||
|
0 ignored issues
–
show
It seems like
$handle can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 150 | } |
||||
| 151 | |||||
| 152 | // ------------------------------------------------------------------------ |
||||
| 153 | |||||
| 154 | /** |
||||
| 155 | * ZipFile::extract |
||||
| 156 | * |
||||
| 157 | * Extract a zip file. |
||||
| 158 | * |
||||
| 159 | * @param string $destinationPath Path of unzip directory. |
||||
| 160 | * |
||||
| 161 | * @return bool Returns TRUE on success or FALSE on failure. |
||||
| 162 | */ |
||||
| 163 | public function extract($destinationPath) |
||||
| 164 | {
|
||||
| 165 | if (extension_loaded('zip')) {
|
||||
| 166 | if (is_file($this->filePath)) {
|
||||
| 167 | $zip = new \ZipArchive; |
||||
| 168 | $contents = $zip->open($this->filePath); |
||||
| 169 | |||||
| 170 | if ($contents === true) {
|
||||
| 171 | if (is_dir($destinationPath)) {
|
||||
| 172 | $zip->extractTo($destinationPath); |
||||
| 173 | $zip->close(); |
||||
| 174 | |||||
| 175 | return true; |
||||
| 176 | } |
||||
| 177 | } |
||||
| 178 | } |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | return false; |
||||
| 182 | } |
||||
| 183 | } |