-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathZipHandler.php
More file actions
169 lines (152 loc) · 4.33 KB
/
Copy pathZipHandler.php
File metadata and controls
169 lines (152 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* This file is a part of the phpMussel\Core package.
* Homepage: https://phpmussel.github.io/
*
* PHPMUSSEL COPYRIGHT 2013 AND BEYOND BY THE PHPMUSSEL TEAM.
*
* License: GNU/GPLv2
* @see LICENSE.txt
*
* This file: Zip handler (last modified: 2026.03.17).
*/
namespace phpMussel\Core;
class ZipHandler extends ArchiveHandler
{
/**
* @var \ZipArchive The zip object.
*/
private $ZipObject;
/**
* @var int The number of files in the archive.
*/
private $NumFiles = 0;
/**
* @var int The current entry index.
*/
private $Index = -1;
/**
* @var array The current entry's attributes.
*/
private $StatIndex = [];
/**
* Construct the zip archive object.
*
* @param string $Pointer
* @return void
*/
public function __construct($Pointer)
{
/** Zip class requirements guard. */
if (!\class_exists('\ZipArchive')) {
$this->ErrorState = 1;
return;
}
/** Bad pointer guard. */
if (!\is_readable($Pointer)) {
$this->ErrorState = 2;
return;
}
$this->ZipObject = new \ZipArchive();
if ($this->ZipObject->open($Pointer) !== true) {
$this->ErrorState = 2;
return;
}
$this->ErrorState = \is_object($this->ZipObject) ? 0 : 2;
$this->NumFiles = $this->ZipObject->numFiles;
}
/**
* Destruct the Zip archive object.
*
* @return void
*/
public function __destruct()
{
if (\is_object($this->ZipObject) && $this->ErrorState === 0) {
$this->ZipObject->close();
}
}
/**
* Return the actual entry in the archive at the current entry pointer.
*
* @param int $Bytes Optionally, how many bytes to read from the entry.
* @return string The entry's content, or an empty string if not available.
*/
public function EntryRead(int $Bytes = -1): string
{
$Actual = $this->EntryActualSize();
if ($Bytes < 0 || $Bytes > $Actual) {
$Bytes = $Actual;
}
return $Bytes > 0 ? $this->ZipObject->getFromIndex($this->Index, $Bytes) : '';
}
/**
* Return the compressed size of the entry at the current entry pointer.
*
* @return int
*/
public function EntryCompressedSize(): int
{
return (int)($this->StatIndex['comp_size'] ?? 0);
}
/**
* Return the actual size of the entry at the current entry pointer.
*
* @return int
*/
public function EntryActualSize(): int
{
return (int)($this->StatIndex['size'] ?? 0);
}
/**
* Return whether the entry at the current entry pointer is a directory.
*
* @return bool True = Is a directory. False = Isn't a directory.
*/
public function EntryIsDirectory(): bool
{
return (!$this->EntryActualSize() && !$this->EntryCompressedSize() && \substr($this->EntryName(), -1) === '/');
}
/**
* Return whether the entry at the current entry pointer is encrypted.
*
* @return bool True = Is encrypted. False = Isn't encrypted.
*/
public function EntryIsEncrypted(): bool
{
return !empty($this->StatIndex['encryption_method']);
}
/**
* Return the reported internal CRC hash for the entry, if it exists.
*
* @return string
*/
public function EntryCRC(): string
{
return (isset($this->StatIndex['crc']) && \is_int($this->StatIndex['crc'])) ? \dechex($this->StatIndex['crc']) : '';
}
/**
* Return the name of the entry at the current entry pointer.
*
* @return string The name of the entry at the current entry pointer, or an
* empty string if there's no entry or if the entry pointer is invalid.
*/
public function EntryName(): string
{
return (isset($this->StatIndex['name']) && \is_string($this->StatIndex['name'])) ? $this->StatIndex['name'] : '';
}
/**
* Move the entry pointer ahead.
*
* @return bool False if there aren't any more entries.
*/
public function EntryNext(): bool
{
$this->Index++;
if ($this->Index < $this->NumFiles) {
$this->StatIndex = $this->ZipObject->statIndex($this->Index);
return true;
}
return false;
}
}