框架升级及修复升级后的bug
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class CentralDirectoryFileHeader
|
||||
{
|
||||
private const SIGNATURE = 0x02014b50;
|
||||
|
||||
public static function generate(
|
||||
int $versionMadeBy,
|
||||
int $versionNeededToExtract,
|
||||
int $generalPurposeBitFlag,
|
||||
CompressionMethod $compressionMethod,
|
||||
DateTimeInterface $lastModificationDateTime,
|
||||
int $crc32,
|
||||
int $compressedSize,
|
||||
int $uncompressedSize,
|
||||
string $fileName,
|
||||
string $extraField,
|
||||
string $fileComment,
|
||||
int $diskNumberStart,
|
||||
int $internalFileAttributes,
|
||||
int $externalFileAttributes,
|
||||
int $relativeOffsetOfLocalHeader,
|
||||
): string {
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: self::SIGNATURE),
|
||||
new PackField(format: 'v', value: $versionMadeBy),
|
||||
new PackField(format: 'v', value: $versionNeededToExtract),
|
||||
new PackField(format: 'v', value: $generalPurposeBitFlag),
|
||||
new PackField(format: 'v', value: $compressionMethod->value),
|
||||
new PackField(format: 'V', value: Time::dateTimeToDosTime($lastModificationDateTime)),
|
||||
new PackField(format: 'V', value: $crc32),
|
||||
new PackField(format: 'V', value: $compressedSize),
|
||||
new PackField(format: 'V', value: $uncompressedSize),
|
||||
new PackField(format: 'v', value: strlen($fileName)),
|
||||
new PackField(format: 'v', value: strlen($extraField)),
|
||||
new PackField(format: 'v', value: strlen($fileComment)),
|
||||
new PackField(format: 'v', value: $diskNumberStart),
|
||||
new PackField(format: 'v', value: $internalFileAttributes),
|
||||
new PackField(format: 'V', value: $externalFileAttributes),
|
||||
new PackField(format: 'V', value: $relativeOffsetOfLocalHeader),
|
||||
) . $fileName . $extraField . $fileComment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
/**
|
||||
* @api
|
||||
*/
|
||||
enum CompressionMethod: int
|
||||
{
|
||||
/**
|
||||
* The file is stored (no compression)
|
||||
*/
|
||||
case STORE = 0x00;
|
||||
|
||||
// 0x01: legacy algorithm - The file is Shrunk
|
||||
// 0x02: legacy algorithm - The file is Reduced with compression factor 1
|
||||
// 0x03: legacy algorithm - The file is Reduced with compression factor 2
|
||||
// 0x04: legacy algorithm - The file is Reduced with compression factor 3
|
||||
// 0x05: legacy algorithm - The file is Reduced with compression factor 4
|
||||
// 0x06: legacy algorithm - The file is Imploded
|
||||
// 0x07: Reserved for Tokenizing compression algorithm
|
||||
|
||||
/**
|
||||
* The file is Deflated
|
||||
*/
|
||||
case DEFLATE = 0x08;
|
||||
|
||||
// /**
|
||||
// * Enhanced Deflating using Deflate64(tm)
|
||||
// */
|
||||
// case DEFLATE_64 = 0x09;
|
||||
|
||||
// /**
|
||||
// * PKWARE Data Compression Library Imploding (old IBM TERSE)
|
||||
// */
|
||||
// case PKWARE = 0x0a;
|
||||
|
||||
// // 0x0b: Reserved by PKWARE
|
||||
|
||||
// /**
|
||||
// * File is compressed using BZIP2 algorithm
|
||||
// */
|
||||
// case BZIP2 = 0x0c;
|
||||
|
||||
// // 0x0d: Reserved by PKWARE
|
||||
|
||||
// /**
|
||||
// * LZMA
|
||||
// */
|
||||
// case LZMA = 0x0e;
|
||||
|
||||
// // 0x0f: Reserved by PKWARE
|
||||
|
||||
// /**
|
||||
// * IBM z/OS CMPSC Compression
|
||||
// */
|
||||
// case IBM_ZOS_CMPSC = 0x10;
|
||||
|
||||
// // 0x11: Reserved by PKWARE
|
||||
|
||||
// /**
|
||||
// * File is compressed using IBM TERSE
|
||||
// */
|
||||
// case IBM_TERSE = 0x12;
|
||||
|
||||
// /**
|
||||
// * IBM LZ77 z Architecture
|
||||
// */
|
||||
// case IBM_LZ77 = 0x13;
|
||||
|
||||
// // 0x14: deprecated (use method 93 for zstd)
|
||||
|
||||
// /**
|
||||
// * Zstandard (zstd) Compression
|
||||
// */
|
||||
// case ZSTD = 0x5d;
|
||||
|
||||
// /**
|
||||
// * MP3 Compression
|
||||
// */
|
||||
// case MP3 = 0x5e;
|
||||
|
||||
// /**
|
||||
// * XZ Compression
|
||||
// */
|
||||
// case XZ = 0x5f;
|
||||
|
||||
// /**
|
||||
// * JPEG variant
|
||||
// */
|
||||
// case JPEG = 0x60;
|
||||
|
||||
// /**
|
||||
// * WavPack compressed data
|
||||
// */
|
||||
// case WAV_PACK = 0x61;
|
||||
|
||||
// /**
|
||||
// * PPMd version I, Rev 1
|
||||
// */
|
||||
// case PPMD_1_1 = 0x62;
|
||||
|
||||
// /**
|
||||
// * AE-x encryption marker
|
||||
// */
|
||||
// case AE_X_ENCRYPTION = 0x63;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class DataDescriptor
|
||||
{
|
||||
private const SIGNATURE = 0x08074b50;
|
||||
|
||||
public static function generate(
|
||||
int $crc32UncompressedData,
|
||||
int $compressedSize,
|
||||
int $uncompressedSize,
|
||||
): string {
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: self::SIGNATURE),
|
||||
new PackField(format: 'V', value: $crc32UncompressedData),
|
||||
new PackField(format: 'V', value: $compressedSize),
|
||||
new PackField(format: 'V', value: $uncompressedSize),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class EndOfCentralDirectory
|
||||
{
|
||||
private const SIGNATURE = 0x06054b50;
|
||||
|
||||
public static function generate(
|
||||
int $numberOfThisDisk,
|
||||
int $numberOfTheDiskWithCentralDirectoryStart,
|
||||
int $numberOfCentralDirectoryEntriesOnThisDisk,
|
||||
int $numberOfCentralDirectoryEntries,
|
||||
int $sizeOfCentralDirectory,
|
||||
int $centralDirectoryStartOffsetOnDisk,
|
||||
string $zipFileComment,
|
||||
): string {
|
||||
/** @psalm-suppress MixedArgument */
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: static::SIGNATURE),
|
||||
new PackField(format: 'v', value: $numberOfThisDisk),
|
||||
new PackField(format: 'v', value: $numberOfTheDiskWithCentralDirectoryStart),
|
||||
new PackField(format: 'v', value: $numberOfCentralDirectoryEntriesOnThisDisk),
|
||||
new PackField(format: 'v', value: $numberOfCentralDirectoryEntries),
|
||||
new PackField(format: 'V', value: $sizeOfCentralDirectory),
|
||||
new PackField(format: 'V', value: $centralDirectoryStartOffsetOnDisk),
|
||||
new PackField(format: 'v', value: strlen($zipFileComment)),
|
||||
) . $zipFileComment;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Exception;
|
||||
|
||||
use DateTimeInterface;
|
||||
use ZipStream\Exception;
|
||||
|
||||
/**
|
||||
* This Exception gets invoked if a DOS time is overflowing
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class DosTimeOverflowException extends Exception
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly DateTimeInterface $dateTime
|
||||
) {
|
||||
parent::__construct('The date ' . $dateTime->format(DateTimeInterface::ATOM) . " can't be represented as DOS time / date.");
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Exception;
|
||||
|
||||
use ZipStream\Exception;
|
||||
|
||||
/**
|
||||
* This Exception gets invoked if a file is not as large as it was specified.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class FileSizeIncorrectException extends Exception
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $expectedSize,
|
||||
public readonly int $actualSize
|
||||
) {
|
||||
parent::__construct("File is {$actualSize} instead of {$expectedSize} bytes large. Adjust `exactSize` parameter.");
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Exception;
|
||||
|
||||
use ZipStream\Exception;
|
||||
|
||||
/**
|
||||
* This Exception gets invoked if a resource like `fread` returns false
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class ResourceActionException extends Exception
|
||||
{
|
||||
/**
|
||||
* @var ?resource
|
||||
*/
|
||||
public $resource;
|
||||
|
||||
/**
|
||||
* @param resource $resource
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly string $function,
|
||||
$resource = null,
|
||||
) {
|
||||
$this->resource = $resource;
|
||||
parent::__construct('Function ' . $function . 'failed on resource.');
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Exception;
|
||||
|
||||
use ZipStream\Exception;
|
||||
|
||||
/**
|
||||
* This Exception gets invoked if a strict simulation is executed and the file
|
||||
* information can't be determined without reading the entire file.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class SimulationFileUnknownException extends Exception
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The details of the strict simulation file could not be determined without reading the entire file.');
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Exception;
|
||||
|
||||
use ZipStream\Exception;
|
||||
|
||||
/**
|
||||
* This Exception gets invoked if a non seekable stream is
|
||||
* provided and zero headers are disabled.
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class StreamNotSeekableException extends Exception
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('enableZeroHeader must be enable to add non seekable streams');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class GeneralPurposeBitFlag
|
||||
{
|
||||
/**
|
||||
* If set, indicates that the file is encrypted.
|
||||
*/
|
||||
public const ENCRYPTED = 1 << 0;
|
||||
|
||||
/**
|
||||
* (For Methods 8 and 9 - Deflating)
|
||||
* Normal (-en) compression option was used.
|
||||
*/
|
||||
public const DEFLATE_COMPRESSION_NORMAL = 0 << 1;
|
||||
|
||||
/**
|
||||
* (For Methods 8 and 9 - Deflating)
|
||||
* Maximum (-exx/-ex) compression option was used.
|
||||
*/
|
||||
public const DEFLATE_COMPRESSION_MAXIMUM = 1 << 1;
|
||||
|
||||
/**
|
||||
* (For Methods 8 and 9 - Deflating)
|
||||
* Fast (-ef) compression option was used.
|
||||
*/
|
||||
public const DEFLATE_COMPRESSION_FAST = 10 << 1;
|
||||
|
||||
/**
|
||||
* (For Methods 8 and 9 - Deflating)
|
||||
* Super Fast (-es) compression option was used.
|
||||
*/
|
||||
public const DEFLATE_COMPRESSION_SUPERFAST = 11 << 1;
|
||||
|
||||
/**
|
||||
* If the compression method used was type 14,
|
||||
* LZMA, then this bit, if set, indicates
|
||||
* an end-of-stream (EOS) marker is used to
|
||||
* mark the end of the compressed data stream.
|
||||
* If clear, then an EOS marker is not present
|
||||
* and the compressed data size must be known
|
||||
* to extract.
|
||||
*/
|
||||
public const LZMA_EOS = 1 << 1;
|
||||
|
||||
/**
|
||||
* If this bit is set, the fields crc-32, compressed
|
||||
* size and uncompressed size are set to zero in the
|
||||
* local header. The correct values are put in the
|
||||
* data descriptor immediately following the compressed
|
||||
* data.
|
||||
*/
|
||||
public const ZERO_HEADER = 1 << 3;
|
||||
|
||||
/**
|
||||
* If this bit is set, this indicates that the file is
|
||||
* compressed patched data.
|
||||
*/
|
||||
public const COMPRESSED_PATCHED_DATA = 1 << 5;
|
||||
|
||||
/**
|
||||
* Strong encryption. If this bit is set, you MUST
|
||||
* set the version needed to extract value to at least
|
||||
* 50 and you MUST also set bit 0. If AES encryption
|
||||
* is used, the version needed to extract value MUST
|
||||
* be at least 51.
|
||||
*/
|
||||
public const STRONG_ENCRYPTION = 1 << 6;
|
||||
|
||||
/**
|
||||
* Language encoding flag (EFS). If this bit is set,
|
||||
* the filename and comment fields for this file
|
||||
* MUST be encoded using UTF-8.
|
||||
*/
|
||||
public const EFS = 1 << 11;
|
||||
|
||||
/**
|
||||
* Set when encrypting the Central Directory to indicate
|
||||
* selected data values in the Local Header are masked to
|
||||
* hide their actual values.
|
||||
*/
|
||||
public const ENCRYPT_CENTRAL_DIRECTORY = 1 << 13;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
use DateTimeInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class LocalFileHeader
|
||||
{
|
||||
private const SIGNATURE = 0x04034b50;
|
||||
|
||||
public static function generate(
|
||||
int $versionNeededToExtract,
|
||||
int $generalPurposeBitFlag,
|
||||
CompressionMethod $compressionMethod,
|
||||
DateTimeInterface $lastModificationDateTime,
|
||||
int $crc32UncompressedData,
|
||||
int $compressedSize,
|
||||
int $uncompressedSize,
|
||||
string $fileName,
|
||||
string $extraField,
|
||||
): string {
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: self::SIGNATURE),
|
||||
new PackField(format: 'v', value: $versionNeededToExtract),
|
||||
new PackField(format: 'v', value: $generalPurposeBitFlag),
|
||||
new PackField(format: 'v', value: $compressionMethod->value),
|
||||
new PackField(format: 'V', value: Time::dateTimeToDosTime($lastModificationDateTime)),
|
||||
new PackField(format: 'V', value: $crc32UncompressedData),
|
||||
new PackField(format: 'V', value: $compressedSize),
|
||||
new PackField(format: 'V', value: $uncompressedSize),
|
||||
new PackField(format: 'v', value: strlen($fileName)),
|
||||
new PackField(format: 'v', value: strlen($extraField)),
|
||||
) . $fileName . $extraField;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
/**
|
||||
* ZipStream execution operation modes
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
enum OperationMode
|
||||
{
|
||||
/**
|
||||
* Stream file into output stream
|
||||
*/
|
||||
case NORMAL;
|
||||
|
||||
/**
|
||||
* Simulate the zip to figure out the resulting file size
|
||||
*
|
||||
* This only supports entries where the file size is known beforehand and
|
||||
* deflation is disabled.
|
||||
*/
|
||||
case SIMULATE_STRICT;
|
||||
|
||||
/**
|
||||
* Simulate the zip to figure out the resulting file size
|
||||
*
|
||||
* If the file size is not known beforehand or deflation is enabled, the
|
||||
* entry streams will be read and rewound.
|
||||
*
|
||||
* If the entry does not support rewinding either, you will not be able to
|
||||
* use the same stream in a later operation mode like `NORMAL`.
|
||||
*/
|
||||
case SIMULATE_LAX;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* TODO: Make class readonly when requiring PHP 8.2 exclusively
|
||||
*/
|
||||
final class PackField
|
||||
{
|
||||
public const MAX_V = 0xFFFFFFFF;
|
||||
|
||||
public const MAX_v = 0xFFFF;
|
||||
|
||||
public function __construct(
|
||||
public readonly string $format,
|
||||
public readonly int|string $value
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create a format string and argument list for pack(), then call
|
||||
* pack() and return the result.
|
||||
*/
|
||||
public static function pack(self ...$fields): string
|
||||
{
|
||||
$fmt = array_reduce($fields, function (string $acc, self $field) {
|
||||
return $acc . $field->format;
|
||||
}, '');
|
||||
|
||||
$args = array_map(function (self $field) {
|
||||
switch ($field->format) {
|
||||
case 'V':
|
||||
if ($field->value > self::MAX_V) {
|
||||
throw new RuntimeException(print_r($field->value, true) . ' is larger than 32 bits');
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
if ($field->value > self::MAX_v) {
|
||||
throw new RuntimeException(print_r($field->value, true) . ' is larger than 16 bits');
|
||||
}
|
||||
break;
|
||||
case 'P': break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return $field->value;
|
||||
}, $fields);
|
||||
|
||||
return pack($fmt, ...$args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Stream;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Stream wrapper that allows writing data to a callback function.
|
||||
*
|
||||
* This wrapper creates a virtual stream that forwards all written data
|
||||
* to a provided callback function, enabling custom output handling
|
||||
* such as streaming to HTTP responses, files, or other destinations.
|
||||
*
|
||||
* @psalm-suppress UnusedClass Used dynamically through stream_wrapper_register
|
||||
*/
|
||||
final class CallbackStreamWrapper
|
||||
{
|
||||
public const PROTOCOL = 'zipcb';
|
||||
|
||||
/** @var resource|null */
|
||||
public $context;
|
||||
|
||||
/** @var array<string, callable(string):void> Map of stream IDs to callback functions */
|
||||
private static array $callbacks = [];
|
||||
|
||||
/** @var string|null Unique identifier for this stream instance */
|
||||
private ?string $id = null;
|
||||
|
||||
/** @var int Current position in the stream */
|
||||
private int $pos = 0;
|
||||
|
||||
/**
|
||||
* Destructor - ensures cleanup even if stream_close() isn't called.
|
||||
* Prevents memory leaks in long-running processes.
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->stream_close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new callback stream.
|
||||
*
|
||||
* @param callable(string):void $callback Function to call with written data
|
||||
* @return resource|false Stream resource or false on failure
|
||||
*/
|
||||
public static function open(callable $callback)
|
||||
{
|
||||
if (!in_array(self::PROTOCOL, stream_get_wrappers(), true)) {
|
||||
if (!stream_wrapper_register(self::PROTOCOL, self::class)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate cryptographically secure unique ID to prevent collisions
|
||||
$id = 'cb_' . bin2hex(random_bytes(16));
|
||||
self::$callbacks[$id] = $callback;
|
||||
|
||||
return fopen(self::PROTOCOL . "://{$id}", 'wb');
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up all registered callbacks (useful for testing).
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function cleanup(): void
|
||||
{
|
||||
self::$callbacks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the stream.
|
||||
*
|
||||
* @param string $path Stream path containing the callback ID
|
||||
* @param string $mode File mode (must contain 'w' for writing)
|
||||
* @param int $options Stream options (required by interface, unused)
|
||||
* @param string|null $opened_path Opened path reference (required by interface, unused)
|
||||
* @return bool True if stream opened successfully
|
||||
* @psalm-suppress UnusedParam $options and $opened_path are required by the stream wrapper interface
|
||||
*/
|
||||
public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
|
||||
{
|
||||
if (!str_contains($mode, 'w')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$host = parse_url($path, PHP_URL_HOST);
|
||||
if ($host === false || $host === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->id = $host;
|
||||
return isset(self::$callbacks[$this->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to the callback.
|
||||
*
|
||||
* @param string $data Data to write
|
||||
* @return int Number of bytes written
|
||||
* @throws RuntimeException If callback execution fails
|
||||
*/
|
||||
public function stream_write(string $data): int
|
||||
{
|
||||
if ($this->id === null) {
|
||||
trigger_error('Stream not properly initialized', E_USER_WARNING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
$callback = self::$callbacks[$this->id] ?? null;
|
||||
if ($callback === null) {
|
||||
trigger_error('Callback not found for stream', E_USER_WARNING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
$callback($data);
|
||||
} catch (Throwable $e) {
|
||||
throw new RuntimeException(
|
||||
'Callback function failed during stream write: ' . $e->getMessage(),
|
||||
0,
|
||||
$e
|
||||
);
|
||||
}
|
||||
|
||||
$length = strlen($data);
|
||||
$this->pos += $length;
|
||||
return $length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current position in stream.
|
||||
*
|
||||
* @return int Current position
|
||||
*/
|
||||
public function stream_tell(): int
|
||||
{
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if stream has reached end of file.
|
||||
*
|
||||
* @return bool Always false for write-only streams
|
||||
*/
|
||||
public function stream_eof(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush stream buffers.
|
||||
*
|
||||
* @return bool Always true (no buffering)
|
||||
*/
|
||||
public function stream_flush(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the stream and clean up callback.
|
||||
*/
|
||||
public function stream_close(): void
|
||||
{
|
||||
if ($this->id !== null) {
|
||||
unset(self::$callbacks[$this->id]);
|
||||
$this->id = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stream statistics.
|
||||
*
|
||||
* @return array<string, mixed> Stream statistics
|
||||
*/
|
||||
public function stream_stat(): array
|
||||
{
|
||||
return [
|
||||
'dev' => 0,
|
||||
'ino' => 0,
|
||||
'mode' => 0o100666, // Regular file, read/write permissions
|
||||
'nlink' => 1,
|
||||
'uid' => 0,
|
||||
'gid' => 0,
|
||||
'rdev' => 0,
|
||||
'size' => $this->pos,
|
||||
'atime' => time(),
|
||||
'mtime' => time(),
|
||||
'ctime' => time(),
|
||||
'blksize' => 4096,
|
||||
'blocks' => ceil($this->pos / 4096),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from stream (not supported - write-only stream).
|
||||
*
|
||||
* @param int $count Number of bytes to read (required by interface, unused)
|
||||
* @return string Always empty string
|
||||
* @psalm-suppress UnusedParam $count is required by the stream wrapper interface
|
||||
*/
|
||||
public function stream_read(int $count): string
|
||||
{
|
||||
trigger_error('Read operations not supported on callback streams', E_USER_WARNING);
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to position in stream (not supported).
|
||||
*
|
||||
* @param int $offset Offset to seek to (required by interface, unused)
|
||||
* @param int $whence Seek mode (required by interface, unused)
|
||||
* @return bool Always false
|
||||
* @psalm-suppress UnusedParam $offset and $whence are required by the stream wrapper interface
|
||||
*/
|
||||
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
|
||||
{
|
||||
trigger_error('Seek operations not supported on callback streams', E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options on stream (not supported).
|
||||
*
|
||||
* @param int $option Option to set (required by interface, unused)
|
||||
* @param int $arg1 First argument (required by interface, unused)
|
||||
* @param int $arg2 Second argument (required by interface, unused)
|
||||
* @return bool Always false
|
||||
* @psalm-suppress UnusedParam All parameters are required by the stream wrapper interface
|
||||
*/
|
||||
public function stream_set_option(int $option, int $arg1, int $arg2): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate stream (not supported).
|
||||
*
|
||||
* @param int $new_size New size (required by interface, unused)
|
||||
* @return bool Always false
|
||||
* @psalm-suppress UnusedParam $new_size is required by the stream wrapper interface
|
||||
*/
|
||||
public function stream_truncate(int $new_size): bool
|
||||
{
|
||||
trigger_error('Truncate operations not supported on callback streams', E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use ZipStream\Exception\DosTimeOverflowException;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class Time
|
||||
{
|
||||
private const DOS_MINIMUM_DATE = '1980-01-01 00:00:00Z';
|
||||
|
||||
public static function dateTimeToDosTime(DateTimeInterface $dateTime): int
|
||||
{
|
||||
$dosMinimumDate = new DateTimeImmutable(self::DOS_MINIMUM_DATE);
|
||||
|
||||
if ($dateTime->getTimestamp() < $dosMinimumDate->getTimestamp()) {
|
||||
throw new DosTimeOverflowException(dateTime: $dateTime);
|
||||
}
|
||||
|
||||
$dateTime = DateTimeImmutable::createFromInterface($dateTime)->sub(new DateInterval('P1980Y'));
|
||||
|
||||
[$year, $month, $day, $hour, $minute, $second] = explode(' ', $dateTime->format('Y n j G i s'));
|
||||
|
||||
return
|
||||
((int) $year << 25)
|
||||
| ((int) $month << 21)
|
||||
| ((int) $day << 16)
|
||||
| ((int) $hour << 11)
|
||||
| ((int) $minute << 5)
|
||||
| ((int) $second >> 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream;
|
||||
|
||||
/**
|
||||
* @api
|
||||
*/
|
||||
enum Version: int
|
||||
{
|
||||
case STORE = 0x000A; // 1.00
|
||||
case DEFLATE = 0x0014; // 2.00
|
||||
case ZIP64 = 0x002D; // 4.50
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Zip64;
|
||||
|
||||
use ZipStream\PackField;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class DataDescriptor
|
||||
{
|
||||
private const SIGNATURE = 0x08074b50;
|
||||
|
||||
public static function generate(
|
||||
int $crc32UncompressedData,
|
||||
int $compressedSize,
|
||||
int $uncompressedSize,
|
||||
): string {
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: self::SIGNATURE),
|
||||
new PackField(format: 'V', value: $crc32UncompressedData),
|
||||
new PackField(format: 'P', value: $compressedSize),
|
||||
new PackField(format: 'P', value: $uncompressedSize),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Zip64;
|
||||
|
||||
use ZipStream\PackField;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class EndOfCentralDirectory
|
||||
{
|
||||
private const SIGNATURE = 0x06064b50;
|
||||
|
||||
public static function generate(
|
||||
int $versionMadeBy,
|
||||
int $versionNeededToExtract,
|
||||
int $numberOfThisDisk,
|
||||
int $numberOfTheDiskWithCentralDirectoryStart,
|
||||
int $numberOfCentralDirectoryEntriesOnThisDisk,
|
||||
int $numberOfCentralDirectoryEntries,
|
||||
int $sizeOfCentralDirectory,
|
||||
int $centralDirectoryStartOffsetOnDisk,
|
||||
string $extensibleDataSector,
|
||||
): string {
|
||||
$recordSize = 44 + strlen($extensibleDataSector); // (length of block - 12) = 44;
|
||||
|
||||
/** @psalm-suppress MixedArgument */
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: static::SIGNATURE),
|
||||
new PackField(format: 'P', value: $recordSize),
|
||||
new PackField(format: 'v', value: $versionMadeBy),
|
||||
new PackField(format: 'v', value: $versionNeededToExtract),
|
||||
new PackField(format: 'V', value: $numberOfThisDisk),
|
||||
new PackField(format: 'V', value: $numberOfTheDiskWithCentralDirectoryStart),
|
||||
new PackField(format: 'P', value: $numberOfCentralDirectoryEntriesOnThisDisk),
|
||||
new PackField(format: 'P', value: $numberOfCentralDirectoryEntries),
|
||||
new PackField(format: 'P', value: $sizeOfCentralDirectory),
|
||||
new PackField(format: 'P', value: $centralDirectoryStartOffsetOnDisk),
|
||||
) . $extensibleDataSector;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Zip64;
|
||||
|
||||
use ZipStream\PackField;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class EndOfCentralDirectoryLocator
|
||||
{
|
||||
private const SIGNATURE = 0x07064b50;
|
||||
|
||||
public static function generate(
|
||||
int $numberOfTheDiskWithZip64CentralDirectoryStart,
|
||||
int $zip64centralDirectoryStartOffsetOnDisk,
|
||||
int $totalNumberOfDisks,
|
||||
): string {
|
||||
/** @psalm-suppress MixedArgument */
|
||||
return PackField::pack(
|
||||
new PackField(format: 'V', value: static::SIGNATURE),
|
||||
new PackField(format: 'V', value: $numberOfTheDiskWithZip64CentralDirectoryStart),
|
||||
new PackField(format: 'P', value: $zip64centralDirectoryStartOffsetOnDisk),
|
||||
new PackField(format: 'V', value: $totalNumberOfDisks),
|
||||
);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Zip64;
|
||||
|
||||
use ZipStream\PackField;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class ExtendedInformationExtraField
|
||||
{
|
||||
private const TAG = 0x0001;
|
||||
|
||||
public static function generate(
|
||||
?int $originalSize = null,
|
||||
?int $compressedSize = null,
|
||||
?int $relativeHeaderOffset = null,
|
||||
?int $diskStartNumber = null,
|
||||
): string {
|
||||
return PackField::pack(
|
||||
new PackField(format: 'v', value: self::TAG),
|
||||
new PackField(
|
||||
format: 'v',
|
||||
value: ($originalSize === null ? 0 : 8)
|
||||
+ ($compressedSize === null ? 0 : 8)
|
||||
+ ($relativeHeaderOffset === null ? 0 : 8)
|
||||
+ ($diskStartNumber === null ? 0 : 4)
|
||||
),
|
||||
...($originalSize === null ? [] : [
|
||||
new PackField(format: 'P', value: $originalSize),
|
||||
]),
|
||||
...($compressedSize === null ? [] : [
|
||||
new PackField(format: 'P', value: $compressedSize),
|
||||
]),
|
||||
...($relativeHeaderOffset === null ? [] : [
|
||||
new PackField(format: 'P', value: $relativeHeaderOffset),
|
||||
]),
|
||||
...($diskStartNumber === null ? [] : [
|
||||
new PackField(format: 'V', value: $diskStartNumber),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZipStream\Zs;
|
||||
|
||||
use ZipStream\PackField;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class ExtendedInformationExtraField
|
||||
{
|
||||
private const TAG = 0x5653;
|
||||
|
||||
public static function generate(): string
|
||||
{
|
||||
return PackField::pack(
|
||||
new PackField(format: 'v', value: self::TAG),
|
||||
new PackField(format: 'v', value: 0x0000),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user