| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\ZipException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Closes the specified directory entry. |
| 9 |
* |
| 10 |
* @param resource $zip_entry A directory entry previously opened zip_entry_open. |
| 11 |
* @throws ZipException |
| 12 |
* |
| 13 |
*/ |
| 14 |
function zip_entry_close($zip_entry): void |
| 15 |
{ |
| 16 |
error_clear_last(); |
| 17 |
$result = \zip_entry_close($zip_entry); |
| 18 |
if ($result === false) { |
| 19 |
throw ZipException::createFromPhpError(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Opens a directory entry in a zip file for reading. |
| 26 |
* |
| 27 |
* @param resource $zip A valid resource handle returned by zip_open. |
| 28 |
* @param resource $zip_entry A directory entry returned by zip_read. |
| 29 |
* @param string|null $mode Any of the modes specified in the documentation of |
| 30 |
* fopen. |
| 31 |
* |
| 32 |
* Currently, mode is ignored and is always |
| 33 |
* "rb". This is due to the fact that zip support |
| 34 |
* in PHP is read only access. |
| 35 |
* |
| 36 |
* @throws ZipException |
| 37 |
* |
| 38 |
*/ |
| 39 |
function zip_entry_open($zip, $zip_entry, ?string $mode = null): void |
| 40 |
{ |
| 41 |
error_clear_last(); |
| 42 |
if ($mode !== null) { |
| 43 |
$result = \zip_entry_open($zip, $zip_entry, $mode); |
| 44 |
} else { |
| 45 |
$result = \zip_entry_open($zip, $zip_entry); |
| 46 |
} |
| 47 |
if ($result === false) { |
| 48 |
throw ZipException::createFromPhpError(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Reads from an open directory entry. |
| 55 |
* |
| 56 |
* @param resource $zip_entry A directory entry returned by zip_read. |
| 57 |
* @param int $length The number of bytes to return. |
| 58 |
* |
| 59 |
* This should be the uncompressed length you wish to read. |
| 60 |
* @return string Returns the data read, empty string on end of a file. |
| 61 |
* @throws ZipException |
| 62 |
* |
| 63 |
*/ |
| 64 |
function zip_entry_read($zip_entry, int $length = 1024): string |
| 65 |
{ |
| 66 |
error_clear_last(); |
| 67 |
$result = \zip_entry_read($zip_entry, $length); |
| 68 |
if ($result === false) { |
| 69 |
throw ZipException::createFromPhpError(); |
| 70 |
} |
| 71 |
return $result; |
| 72 |
} |
| 73 |
|