1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-27 08:54:25 +02:00

minor fixes, ZipEntry tests

This commit is contained in:
wapplay
2020-01-06 11:53:17 +03:00
parent 70bb598fa6
commit a16b0e7c15
21 changed files with 1906 additions and 141 deletions

View File

@@ -5,6 +5,21 @@ namespace PhpZip\Util;
/**
* Convert unix timestamp values to DOS date/time values and vice versa.
*
* The DOS date/time format is a bitmask:
*
* 24 16 8 0
* +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
* |Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s|
* +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
* \___________/\________/\_________/ \________/\____________/\_________/
* year month day hour minute second
*
* The year is stored as an offset from 1980.
* Seconds are stored in two-second increments.
* (So if the "second" value is 15, it actually represents 30 seconds.)
*
* @see https://docs.microsoft.com/ru-ru/windows/win32/api/winbase/nf-winbase-filetimetodosdatetime?redirectedfrom=MSDN
*
* @author Ne-Lexa alexey@nelexa.ru
* @license MIT
*
@@ -31,21 +46,21 @@ class DateTimeConverter
*
* @return int Unix timestamp
*/
public static function toUnixTimestamp($dosTime)
public static function msDosToUnix($dosTime)
{
if ($dosTime < self::MIN_DOS_TIME) {
$dosTime = self::MIN_DOS_TIME;
if ($dosTime <= self::MIN_DOS_TIME) {
$dosTime = 0;
} elseif ($dosTime > self::MAX_DOS_TIME) {
$dosTime = self::MAX_DOS_TIME;
}
// date_default_timezone_set('UTC');
return mktime(
($dosTime >> 11) & 0x1f, // hour
($dosTime >> 5) & 0x3f, // minute
2 * ($dosTime & 0x1f), // second
($dosTime >> 21) & 0x0f, // month
($dosTime >> 16) & 0x1f, // day
1980 + (($dosTime >> 25) & 0x7f) // year
(($dosTime >> 11) & 0x1f), // hours
(($dosTime >> 5) & 0x3f), // minutes
(($dosTime << 1) & 0x3e), // seconds
(($dosTime >> 21) & 0x0f), // month
(($dosTime >> 16) & 0x1f), // day
((($dosTime >> 25) & 0x7f) + 1980) // year
);
}
@@ -59,22 +74,26 @@ class DateTimeConverter
* rounded down to even seconds
* and is in between DateTimeConverter::MIN_DOS_TIME and DateTimeConverter::MAX_DOS_TIME
*/
public static function toDosTime($unixTimestamp)
public static function unixToMsDos($unixTimestamp)
{
if ($unixTimestamp < 0) {
throw new \InvalidArgumentException('Negative unix timestamp: ' . $unixTimestamp);
}
$date = getdate($unixTimestamp);
$dosTime = (
(($date['year'] - 1980) << 25) |
($date['mon'] << 21) |
($date['mday'] << 16) |
($date['hours'] << 11) |
($date['minutes'] << 5) |
($date['seconds'] >> 1)
);
if ($date['year'] < 1980) {
return self::MIN_DOS_TIME;
if ($dosTime <= self::MIN_DOS_TIME) {
$dosTime = 0;
}
$date['year'] -= 1980;
return $date['year'] << 25 | $date['mon'] << 21 |
$date['mday'] << 16 | $date['hours'] << 11 |
$date['minutes'] << 5 | $date['seconds'] >> 1;
return $dosTime;
}
}

View File

@@ -9,17 +9,6 @@ namespace PhpZip\Util;
*/
final class StringUtil
{
/**
* @param string $haystack
* @param string $needle
*
* @return bool
*/
public static function startsWith($haystack, $needle)
{
return $needle === '' || strrpos($haystack, $needle, -\strlen($haystack)) !== false;
}
/**
* @param string $haystack
* @param string $needle
@@ -28,8 +17,13 @@ final class StringUtil
*/
public static function endsWith($haystack, $needle)
{
return $needle === '' || (($temp = \strlen($haystack) - \strlen($needle)) >= 0
&& strpos($haystack, $needle, $temp) !== false);
$length = \strlen($needle);
if ($length === 0) {
return true;
}
return substr($haystack, -$length) === $needle;
}
/**