mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-16 12:14:37 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
42c0fc59df | ||
|
6688f474b5 | ||
|
810b7ca741 | ||
|
115dfd3b52 | ||
|
183274d6da | ||
|
f99c0278fd | ||
|
1b065c4cca |
@@ -1,9 +0,0 @@
|
||||
engines:
|
||||
duplication:
|
||||
enabled: true
|
||||
config:
|
||||
languages:
|
||||
- php
|
||||
exclude_paths:
|
||||
- tests/
|
||||
- vendor/
|
11
.travis.yml
11
.travis.yml
@@ -4,7 +4,6 @@ php:
|
||||
- '5.6'
|
||||
- '7.0'
|
||||
- '7.1'
|
||||
- hhvm
|
||||
- nightly
|
||||
|
||||
# cache vendor dirs
|
||||
@@ -13,10 +12,6 @@ cache:
|
||||
- vendor
|
||||
- $HOME/.composer/cache
|
||||
|
||||
addons:
|
||||
code_climate:
|
||||
repo_token: 486a09d58d663450146c53c81c6c64938bcf3bb0b7c8ddebdc125fe97c18213a
|
||||
|
||||
install:
|
||||
- travis_retry composer self-update && composer --version
|
||||
- travis_retry composer install --prefer-dist --no-interaction
|
||||
@@ -26,8 +21,4 @@ before_script:
|
||||
|
||||
script:
|
||||
- composer validate --no-check-lock
|
||||
- vendor/bin/phpunit -v -c bootstrap.xml --coverage-clover build/logs/clover.xml
|
||||
|
||||
after_success:
|
||||
- vendor/bin/test-reporter
|
||||
|
||||
- vendor/bin/phpunit -v -c bootstrap.xml
|
||||
|
@@ -1,3 +1,6 @@
|
||||
## 3.0.3 (2017-11-11)
|
||||
Fix bug issue #8 - Error if the file is empty.
|
||||
|
||||
## 3.0.0 (2017-03-15)
|
||||
Merge `ZipOutputFile` with ZipFile and optimize the zip archive update.
|
||||
|
||||
|
@@ -6,7 +6,6 @@
|
||||
[](https://packagist.org/packages/nelexa/zip)
|
||||
[](https://packagist.org/packages/nelexa/zip)
|
||||
[](https://php.net/)
|
||||
[](https://codeclimate.com/github/Ne-Lexa/php-zip/coverage)
|
||||
[](https://packagist.org/packages/nelexa/zip)
|
||||
|
||||
Table of contents
|
||||
|
@@ -24,7 +24,7 @@
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"php-64bit": "^5.5 || ^7.0"
|
||||
"php": "^5.5 || ^7.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
@@ -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.
|
||||
|
@@ -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
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip\Model\Entry;
|
||||
|
||||
use PhpZip\Exception\InvalidArgumentException;
|
||||
@@ -257,6 +258,7 @@ abstract class ZipAbstractEntry implements ZipEntry
|
||||
public function setCompressedSize($compressedSize)
|
||||
{
|
||||
if (self::UNKNOWN != $compressedSize) {
|
||||
$compressedSize = sprintf('%u', $compressedSize);
|
||||
if (0 > $compressedSize || $compressedSize > 0x7fffffffffffffff) {
|
||||
throw new ZipException("Compressed size out of range - " . $this->name);
|
||||
}
|
||||
@@ -285,6 +287,7 @@ abstract class ZipAbstractEntry implements ZipEntry
|
||||
public function setSize($size)
|
||||
{
|
||||
if (self::UNKNOWN != $size) {
|
||||
$size = sprintf('%u', $size);
|
||||
if (0 > $size || $size > 0x7fffffffffffffff) {
|
||||
throw new ZipException("Uncompressed Size out of range - " . $this->name);
|
||||
}
|
||||
@@ -310,6 +313,7 @@ abstract class ZipAbstractEntry implements ZipEntry
|
||||
*/
|
||||
public function setOffset($offset)
|
||||
{
|
||||
$offset = sprintf('%u', $offset);
|
||||
if (0 > $offset || $offset > 0x7fffffffffffffff) {
|
||||
throw new ZipException("Offset out of range - " . $this->name);
|
||||
}
|
||||
@@ -485,7 +489,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 +510,31 @@ 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)
|
||||
{
|
||||
$dosTime = sprintf('%u', $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.
|
||||
*
|
||||
@@ -530,6 +559,7 @@ abstract class ZipAbstractEntry implements ZipEntry
|
||||
{
|
||||
$known = self::UNKNOWN != $externalAttributes;
|
||||
if ($known) {
|
||||
$externalAttributes = sprintf('%u', $externalAttributes);
|
||||
if (0x00000000 > $externalAttributes || $externalAttributes > 0xffffffff) {
|
||||
throw new ZipException("external file attributes out of range - " . $this->name);
|
||||
}
|
||||
@@ -823,6 +853,7 @@ abstract class ZipAbstractEntry implements ZipEntry
|
||||
*/
|
||||
public function setCrc($crc)
|
||||
{
|
||||
$crc = sprintf('%u', $crc);
|
||||
if (0x00000000 > $crc || $crc > 0xffffffff) {
|
||||
throw new ZipException("CRC-32 out of range - " . $this->name);
|
||||
}
|
||||
|
@@ -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
|
||||
@@ -258,7 +258,7 @@ abstract class ZipNewEntry extends ZipAbstractEntry
|
||||
} else {
|
||||
fwrite($outputStream, pack('VV', $this->getCompressedSize(), $this->getSize()));
|
||||
}
|
||||
} elseif ($this->getCompressedSize() !== $compressedSize) {
|
||||
} elseif ($this->getCompressedSize() != $compressedSize) {
|
||||
throw new ZipException($this->getName()
|
||||
. " (expected compressed entry size of "
|
||||
. $this->getCompressedSize() . " bytes, but is actually " . $compressedSize . " bytes)");
|
||||
|
@@ -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']);
|
||||
@@ -151,7 +151,10 @@ class ZipReadEntry extends ZipAbstractEntry
|
||||
fseek($this->inputStream, $pos);
|
||||
|
||||
// Get raw entry content
|
||||
$content = fread($this->inputStream, $this->getCompressedSize());
|
||||
$content = '';
|
||||
if ($this->getCompressedSize() > 0) {
|
||||
$content = fread($this->inputStream, $this->getCompressedSize());
|
||||
}
|
||||
|
||||
// Strong Encryption Specification - WinZip AES
|
||||
if ($this->isEncrypted()) {
|
||||
@@ -282,7 +285,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
|
||||
|
@@ -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.
|
||||
*
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip;
|
||||
|
||||
use PhpZip\Exception\ZipAuthenticationException;
|
||||
@@ -447,7 +448,8 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
public function testDeleteNewEntry(){
|
||||
public function testDeleteNewEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['entry1'] = '';
|
||||
$zipFile['entry2'] = '';
|
||||
@@ -466,7 +468,8 @@ class ZipFileTest extends ZipTestCase
|
||||
* @expectedException \PhpZip\Exception\ZipNotFoundEntry
|
||||
* @expectedExceptionMessage Not found entry entry
|
||||
*/
|
||||
public function testDeleteFromNameNotFoundEntry(){
|
||||
public function testDeleteFromNameNotFoundEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->deleteFromName('entry');
|
||||
}
|
||||
@@ -790,17 +793,19 @@ class ZipFileTest extends ZipTestCase
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid compression level. Minimum level -1. Maximum level 9
|
||||
*/
|
||||
public function testSetInvalidCompressionLevel(){
|
||||
public function testSetInvalidCompressionLevel()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->setCompressionLevel(-2);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* /**
|
||||
* @expectedException \PhpZip\Exception\InvalidArgumentException
|
||||
* @expectedExceptionMessage Invalid compression level. Minimum level -1. Maximum level 9
|
||||
*/
|
||||
public function testSetInvalidCompressionLevel2(){
|
||||
public function testSetInvalidCompressionLevel2()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->setCompressionLevel(10);
|
||||
}
|
||||
@@ -817,11 +822,6 @@ class ZipFileTest extends ZipTestCase
|
||||
'test empty/dir' => null,
|
||||
];
|
||||
|
||||
$extractPath = sys_get_temp_dir() . '/zipExtract' . uniqid();
|
||||
if (!is_dir($extractPath)) {
|
||||
mkdir($extractPath, 0755, true);
|
||||
}
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
foreach ($entries as $entryName => $value) {
|
||||
if ($value === null) {
|
||||
@@ -833,10 +833,12 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->extractTo($extractPath);
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
foreach ($entries as $entryName => $value) {
|
||||
$fullExtractedFilename = $extractPath . DIRECTORY_SEPARATOR . $entryName;
|
||||
$fullExtractedFilename = $this->outputDirname . DIRECTORY_SEPARATOR . $entryName;
|
||||
if ($value === null) {
|
||||
self::assertTrue(is_dir($fullExtractedFilename));
|
||||
self::assertTrue(FilesUtil::isEmptyDir($fullExtractedFilename));
|
||||
@@ -847,8 +849,6 @@ class ZipFileTest extends ZipTestCase
|
||||
}
|
||||
}
|
||||
$zipFile->close();
|
||||
|
||||
FilesUtil::removeDir($extractPath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -876,11 +876,7 @@ class ZipFileTest extends ZipTestCase
|
||||
'test empty/dir2/'
|
||||
];
|
||||
|
||||
$extractPath = sys_get_temp_dir() . '/zipExtractTest';
|
||||
if (is_dir($extractPath)) {
|
||||
FilesUtil::removeDir($extractPath);
|
||||
}
|
||||
self::assertTrue(mkdir($extractPath, 0755, true));
|
||||
self::assertTrue(mkdir($this->outputDirname, 0755, true));
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->addAll($entries);
|
||||
@@ -888,10 +884,10 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->extractTo($extractPath, $extractEntries);
|
||||
$zipFile->extractTo($this->outputDirname, $extractEntries);
|
||||
|
||||
foreach ($entries as $entryName => $value) {
|
||||
$fullExtractFilename = $extractPath . DIRECTORY_SEPARATOR . $entryName;
|
||||
$fullExtractFilename = $this->outputDirname . DIRECTORY_SEPARATOR . $entryName;
|
||||
if (in_array($entryName, $extractEntries)) {
|
||||
if ($value === null) {
|
||||
self::assertTrue(is_dir($fullExtractFilename));
|
||||
@@ -909,12 +905,11 @@ class ZipFileTest extends ZipTestCase
|
||||
}
|
||||
}
|
||||
}
|
||||
self::assertFalse(is_file($extractPath . DIRECTORY_SEPARATOR . 'test/test/test.txt'));
|
||||
$zipFile->extractTo($extractPath, 'test/test/test.txt');
|
||||
self::assertTrue(is_file($extractPath . DIRECTORY_SEPARATOR . 'test/test/test.txt'));
|
||||
self::assertFalse(is_file($this->outputDirname . DIRECTORY_SEPARATOR . 'test/test/test.txt'));
|
||||
$zipFile->extractTo($this->outputDirname, 'test/test/test.txt');
|
||||
self::assertTrue(is_file($this->outputDirname . DIRECTORY_SEPARATOR . 'test/test/test.txt'));
|
||||
|
||||
$zipFile->close();
|
||||
FilesUtil::removeDir($extractPath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -958,15 +953,11 @@ class ZipFileTest extends ZipTestCase
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$extractPath = sys_get_temp_dir() . '/zipExtractTest';
|
||||
if (is_dir($extractPath)) {
|
||||
FilesUtil::removeDir($extractPath);
|
||||
}
|
||||
self::assertTrue(mkdir($extractPath, 0444, true));
|
||||
self::assertTrue(chmod($extractPath, 0444));
|
||||
self::assertTrue(mkdir($this->outputDirname, 0444, true));
|
||||
self::assertTrue(chmod($this->outputDirname, 0444));
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
$zipFile->extractTo($extractPath);
|
||||
$zipFile->extractTo($this->outputDirname);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1059,7 +1050,8 @@ class ZipFileTest extends ZipTestCase
|
||||
* @expectedException \PhpZip\Exception\ZipException
|
||||
* @expectedExceptionMessage Invalid encryption method
|
||||
*/
|
||||
public function testSetEncryptionMethodInvalid(){
|
||||
public function testSetEncryptionMethodInvalid()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$encryptionMethod = 9999;
|
||||
$zipFile->withNewPassword('pass', $encryptionMethod);
|
||||
@@ -1539,14 +1531,10 @@ class ZipFileTest extends ZipTestCase
|
||||
*/
|
||||
public function testSaveAsFileNotWritable()
|
||||
{
|
||||
$this->outputFilename = sys_get_temp_dir() . '/zipExtractTest';
|
||||
if (is_dir($this->outputFilename)) {
|
||||
FilesUtil::removeDir($this->outputFilename);
|
||||
}
|
||||
self::assertTrue(mkdir($this->outputFilename, 0444, true));
|
||||
self::assertTrue(chmod($this->outputFilename, 0444));
|
||||
self::assertTrue(mkdir($this->outputDirname, 0444, true));
|
||||
self::assertTrue(chmod($this->outputDirname, 0444));
|
||||
|
||||
$this->outputFilename .= '/' . uniqid() . '.zip';
|
||||
$this->outputFilename = $this->outputDirname . DIRECTORY_SEPARATOR . basename($this->outputFilename);
|
||||
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
@@ -1699,7 +1687,8 @@ class ZipFileTest extends ZipTestCase
|
||||
* @expectedException \PhpZip\Exception\ZipNotFoundEntry
|
||||
* @expectedExceptionMessage Zip entry bad entry name not found
|
||||
*/
|
||||
public function testNotFoundEntry(){
|
||||
public function testNotFoundEntry()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile['bad entry name'];
|
||||
}
|
||||
@@ -1761,7 +1750,8 @@ class ZipFileTest extends ZipTestCase
|
||||
* @expectedException \PhpZip\Exception\ZipException
|
||||
* @expectedExceptionMessage input stream is null
|
||||
*/
|
||||
public function testRewriteNullStream(){
|
||||
public function testRewriteNullStream()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$zipFile->rewrite();
|
||||
}
|
||||
@@ -1822,6 +1812,19 @@ class ZipFileTest extends ZipTestCase
|
||||
self::assertTrue($result);
|
||||
}
|
||||
|
||||
public function testEmptyContents()
|
||||
{
|
||||
$zipFile = new ZipFile();
|
||||
$contents = '';
|
||||
$zipFile->addFromString('file', $contents);
|
||||
$zipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
$zipFile->openFile($this->outputFilename);
|
||||
self::assertEquals($zipFile['file'], $contents);
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test support ZIP64 ext (slow test - normal).
|
||||
* Create > 65535 files in archive and open and extract to /dev/null.
|
||||
|
Reference in New Issue
Block a user