1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-10-19 00:56:25 +02:00

fix incorrect time in archive

This commit is contained in:
Ne-Lexa
2017-03-15 19:03:04 +03:00
parent 67fc1ea7ad
commit 1b065c4cca
8 changed files with 94 additions and 52 deletions

View File

@@ -155,12 +155,12 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
if ($this->entry->getGeneralPurposeBitFlag(ZipEntry::GPBF_DATA_DESCRIPTOR)) {
// compare against the file type from extended local headers
$checkByte = ($this->entry->getTime() >> 8) & 0xff;
$checkByte = ($this->entry->getDosTime() >> 8) & 0xff;
} else {
// compare against the CRC otherwise
$checkByte = ($this->entry->getCrc() >> 24) & 0xff;
}
if ($headerBytes[11] !== $checkByte) {
if ($byte !== $checkByte) {
throw new ZipAuthenticationException("Bad password for entry " . $this->entry->getName());
}
@@ -192,9 +192,9 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
*/
public function encrypt($data)
{
$crc = ($this->entry->isDataDescriptorRequired() ?
($this->entry->getTime() & 0x0000ffff) << 16 :
$this->entry->getCrc());
$crc = $this->entry->isDataDescriptorRequired() ?
($this->entry->getDosTime() & 0x0000ffff) << 16 :
$this->entry->getCrc();
$headerBytes = CryptoUtil::randomBytes(self::STD_DEC_HDR_SIZE);
// Initialize again since the generated bytes were encrypted.

View File

@@ -166,6 +166,18 @@ class CentralDirectory
return $this->entries[$entryName];
}
/**
* @param string $entryName
* @return ZipEntry
* @throws ZipNotFoundEntry
*/
public function getModifiedEntry($entryName){
if (!isset($this->modifiedEntries[$entryName])) {
throw new ZipNotFoundEntry('Zip modified entry ' . $entryName . ' not found');
}
return $this->modifiedEntries[$entryName];
}
/**
* @return EndOfCentralDirectory
*/
@@ -420,7 +432,7 @@ class CentralDirectory
// compression method 2 bytes
$entry->getMethod(),
// last mod file datetime 4 bytes
$entry->getTime(),
$entry->getDosTime(),
// crc-32 4 bytes
$entry->getCrc(),
// compressed size 4 bytes

View File

@@ -485,7 +485,7 @@ abstract class ZipAbstractEntry implements ZipEntry
if (!$this->isInit(self::BIT_DATE_TIME)) {
return self::UNKNOWN;
}
return DateTimeConverter::toUnixTimestamp($this->dosTime & 0xffffffff);
return DateTimeConverter::toUnixTimestamp($this->getDosTime());
}
/**
@@ -506,6 +506,30 @@ abstract class ZipAbstractEntry implements ZipEntry
return $this;
}
/**
* Get Dos Time
*
* @return int
*/
public function getDosTime()
{
return $this->dosTime & 0xffffffff;
}
/**
* Set Dos Time
* @param int $dosTime
* @throws ZipException
*/
public function setDosTime($dosTime)
{
if (0x00000000 > $dosTime || $dosTime > 0xffffffff) {
throw new ZipException('DosTime out of range');
}
$this->dosTime = $dosTime;
$this->setInit(self::BIT_DATE_TIME, true);
}
/**
* Returns the external file attributes.
*

View File

@@ -218,7 +218,7 @@ abstract class ZipNewEntry extends ZipAbstractEntry
$this->getMethod(),
// last mod file time 2 bytes
// last mod file date 2 bytes
$this->getTime(),
$this->getDosTime(),
// crc-32 4 bytes
$dd ? 0 : $this->getCrc(),
// compressed size 4 bytes

View File

@@ -96,7 +96,7 @@ class ZipReadEntry extends ZipAbstractEntry
$this->setPlatform($data['versionMadeBy'] >> 8);
$this->setGeneralPurposeBitFlags($data['gpbf']);
$this->setMethod($data['rawMethod']);
$this->setTime($data['rawTime']);
$this->setDosTime($data['rawTime']);
$this->setCrc($data['rawCrc']);
$this->setCompressedSize($data['rawCompressedSize']);
$this->setSize($data['rawSize']);
@@ -282,7 +282,7 @@ class ZipReadEntry extends ZipAbstractEntry
$this->getMethod(),
// last mod file time 2 bytes
// last mod file date 2 bytes
$this->getTime(),
$this->getDosTime(),
// crc-32 4 bytes
$dd ? 0 : $this->getCrc(),
// compressed size 4 bytes

View File

@@ -281,6 +281,20 @@ interface ZipEntry
*/
public function setTime($unixTimestamp);
/**
* Get Dos Time
*
* @return int
*/
public function getDosTime();
/**
* Set Dos Time
* @param int $dosTime
* @throws ZipException
*/
public function setDosTime($dosTime);
/**
* Returns the external file attributes.
*

View File

@@ -461,6 +461,9 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator
$localName = basename($filename);
}
$this->addFromStream($handle, $localName, $compressionMethod);
$this->centralDirectory
->getModifiedEntry($localName)
->setTime(filemtime($filename));
return $this;
}