AbstractUnicodeExtraField.php
3.1 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
<?php
namespace PhpZip\Model\Extra\Fields;
use PhpZip\Exception\ZipException;
use PhpZip\Model\Extra\ZipExtraField;
use PhpZip\Model\ZipEntry;
/**
* A common base class for Unicode extra information extra fields.
*/
abstract class AbstractUnicodeExtraField implements ZipExtraField
{
const DEFAULT_VERSION = 0x01;
/** @var int */
private $crc32;
/** @var string */
private $unicodeValue;
/**
* @param int $crc32
* @param string $unicodeValue
*/
public function __construct($crc32, $unicodeValue)
{
$this->crc32 = (int) $crc32;
$this->unicodeValue = (string) $unicodeValue;
}
/**
* @return int the CRC32 checksum of the filename or comment as
* encoded in the central directory of the zip file
*/
public function getCrc32()
{
return $this->crc32;
}
/**
* @param int $crc32
*/
public function setCrc32($crc32)
{
$this->crc32 = (int) $crc32;
}
/**
* @return string
*/
public function getUnicodeValue()
{
return $this->unicodeValue;
}
/**
* @param string $unicodeValue the UTF-8 encoded name to set
*/
public function setUnicodeValue($unicodeValue)
{
$this->unicodeValue = $unicodeValue;
}
/**
* Populate data from this array as if it was in local file data.
*
* @param string $buffer the buffer to read data from
* @param ZipEntry|null $entry
*
* @throws ZipException on error
*
* @return static
*/
public static function unpackLocalFileData($buffer, ZipEntry $entry = null)
{
if (\strlen($buffer) < 5) {
throw new ZipException('Unicode path extra data must have at least 5 bytes.');
}
$data = unpack('Cversion/Vcrc32', $buffer);
if ($data['version'] !== self::DEFAULT_VERSION) {
throw new ZipException(sprintf('Unsupported version [%d] for Unicode path extra data.', $data['version']));
}
$unicodeValue = substr($buffer, 5);
return new static($data['crc32'], $unicodeValue);
}
/**
* Populate data from this array as if it was in central directory data.
*
* @param string $buffer the buffer to read data from
* @param ZipEntry|null $entry
*
* @throws ZipException on error
*
* @return static
*/
public static function unpackCentralDirData($buffer, ZipEntry $entry = null)
{
return self::unpackLocalFileData($buffer, $entry);
}
/**
* The actual data to put into local file data - without Header-ID
* or length specifier.
*
* @return string the data
*/
public function packLocalFileData()
{
return pack(
'CV',
self::DEFAULT_VERSION,
$this->crc32
) .
$this->unicodeValue;
}
/**
* The actual data to put into central directory - without Header-ID or
* length specifier.
*
* @return string the data
*/
public function packCentralDirData()
{
return $this->packLocalFileData();
}
}