mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-10-10 21:04:36 +02:00
solving problems with lack of memory, the new ZipReader and ZipWriter class, adds .phpstorm.meta.php
#13 #16 #27 #31 #41
This commit is contained in:
137
src/Model/Extra/Fields/AbstractUnicodeExtraField.php
Normal file
137
src/Model/Extra/Fields/AbstractUnicodeExtraField.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $unicodeValue
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function create($unicodeValue)
|
||||
{
|
||||
return new static(crc32($unicodeValue), $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
$this->crc32 = crc32($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.');
|
||||
}
|
||||
|
||||
$version = unpack('C', $buffer)[1];
|
||||
|
||||
if ($version !== self::DEFAULT_VERSION) {
|
||||
throw new ZipException(sprintf('Unsupported version [%d] for UniCode path extra data.', $version));
|
||||
}
|
||||
|
||||
$crc32 = unpack('V', substr($buffer, 1))[1];
|
||||
$unicodeValue = substr($buffer, 5);
|
||||
|
||||
return new static($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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user