1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-30 02:09:50 +02:00

12 Commits

Author SHA1 Message Date
wapplay
52ce79d4d2 Merge branch 'release/3.3.1' 2020-04-01 16:33:45 +03:00
Ne-Lexa
decf4f5095 Merge pull request #47 from chx/chx-patch-1
Do not pin random_compat version
2020-04-01 16:29:06 +03:00
chx
70d8b8200b Do not pin random_compat version 2020-03-23 09:49:26 -07:00
Ne-Lexa
8fdc21eece Merge branch 'release/3.3.0' 2020-02-04 12:01:18 +03:00
Ne-Lexa
74c0a49057 Merge tag '3.3.0' into develop
Tagging version 3.3.0 3.3.0
2020-02-04 12:01:18 +03:00
Ne-Lexa
ae6d47f47a removed test ZipInfo::__toString() (depends on time zone) 2020-02-04 11:58:47 +03:00
Ne-Lexa
b3b676e3af Merge branch 'release/3.3.0' 2020-02-04 11:47:43 +03:00
Ne-Lexa
f3d769739b Merge tag '3.2.2' into develop
Tagging hotfix 3.2.2 3.2.2

# Conflicts:
#	.travis.yml
#	tests/ZipFileTest.php
2020-02-04 11:36:29 +03:00
Ne-Lexa
074443dbc4 Merge branch 'hotfix/3.2.2' 2020-02-04 11:29:58 +03:00
Ne-Lexa
cbb693213e fix replace contents by file 2020-02-04 11:18:32 +03:00
Ne-Lexa
820c63c30f ZipInfo tests 2020-01-31 13:42:19 +03:00
Ne-Lexa
2235de6b35 Merge branch 'feature/zipcontainer-override-support' into develop 2020-01-31 12:13:16 +03:00
6 changed files with 183 additions and 5 deletions

View File

@@ -24,7 +24,7 @@
"php": "^5.5.9 || ^7.0",
"ext-zlib": "*",
"psr/http-message": "^1.0",
"paragonie/random_compat": ">=1 <9.99",
"paragonie/random_compat": "*",
"symfony/finder": "^3.0|^4.0|^5.0"
},
"require-dev": {

View File

@@ -4,6 +4,7 @@ namespace PhpZip\Model\Data;
use PhpZip\Exception\ZipException;
use PhpZip\Model\ZipData;
use PhpZip\Model\ZipEntry;
/**
* Class ZipFileData.
@@ -16,11 +17,12 @@ class ZipFileData implements ZipData
/**
* ZipStringData constructor.
*
* @param ZipEntry $zipEntry
* @param \SplFileInfo $fileInfo
*
* @throws ZipException
*/
public function __construct(\SplFileInfo $fileInfo)
public function __construct(ZipEntry $zipEntry, \SplFileInfo $fileInfo)
{
if (!$fileInfo->isFile()) {
throw new ZipException('$fileInfo is not a file.');
@@ -31,6 +33,7 @@ class ZipFileData implements ZipData
}
$this->file = $fileInfo;
$zipEntry->setUncompressedSize($fileInfo->getSize());
}
/**

View File

@@ -142,7 +142,9 @@ class ZipFile implements ZipFileInterface
}
if (!($handle = fopen('php://temp', 'r+b'))) {
// @codeCoverageIgnoreStart
throw new ZipException("Can't open temp stream.");
// @codeCoverageIgnoreEnd
}
fwrite($handle, $data);
rewind($handle);
@@ -459,7 +461,9 @@ class ZipFile implements ZipFileInterface
}
if (!mkdir($dir, $dirMode, true) && !is_dir($dir)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
// @codeCoverageIgnoreEnd
}
chmod($dir, $dirMode);
}
@@ -495,6 +499,7 @@ class ZipFile implements ZipFileInterface
/** @noinspection PhpUsageOfSilenceOperatorInspection */
if (!($handle = @fopen($file, 'w+b'))) {
// @codeCoverageIgnoreStart
throw new ZipException(
sprintf(
'Cannot extract zip entry %s. File %s cannot open for write.',
@@ -502,6 +507,7 @@ class ZipFile implements ZipFileInterface
$file
)
);
// @codeCoverageIgnoreEnd
}
try {
@@ -705,10 +711,9 @@ class ZipFile implements ZipFileInterface
ZipCompressionMethod::DEFLATED;
}
$zipEntry->setUncompressedSize($file->getSize());
$zipEntry->setCompressionMethod($compressionMethod);
$zipData = new ZipFileData($file);
$zipData = new ZipFileData($zipEntry, $file);
} elseif ($file->isDir()) {
$zipEntry->setCompressionMethod(ZipCompressionMethod::STORED);
$zipEntry->setUncompressedSize(0);

View File

@@ -1571,9 +1571,10 @@ class ZipEntryTest extends TestCase
public function testClone()
{
$newUnixExtra = new NewUnixExtraField();
$zipData = new ZipFileData(new \SplFileInfo(__FILE__));
$zipEntry = new ZipEntry('entry');
$zipData = new ZipFileData($zipEntry, new \SplFileInfo(__FILE__));
$zipEntry->addExtraField($newUnixExtra);
$zipEntry->setData($zipData);

View File

@@ -10,6 +10,7 @@ use PhpZip\Exception\InvalidArgumentException;
use PhpZip\Exception\ZipEntryNotFoundException;
use PhpZip\Exception\ZipException;
use PhpZip\Exception\ZipUnsupportMethodException;
use PhpZip\Model\Data\ZipFileData;
use PhpZip\Model\ZipEntry;
use PhpZip\Model\ZipInfo;
use PhpZip\Util\FilesUtil;
@@ -2458,4 +2459,56 @@ class ZipFileTest extends ZipTestCase
}
$zipFile->close();
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testNoData()
{
$this->setExpectedException(ZipException::class, 'No data for zip entry file');
$entryName = 'file';
$zipFile = new ZipFile();
try {
$zipFile[$entryName] = '';
$zipEntry = $zipFile->getEntry($entryName);
$zipEntry->setData(null);
$zipFile->getEntryContents($entryName);
} finally {
$zipFile->close();
}
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testReplaceEntryContentsByFile()
{
$entryName = basename(__FILE__);
$zipFile = new ZipFile();
$zipFile[$entryName] = 'contents';
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$zipFile->openFile($this->outputFilename);
$entry = $zipFile->getEntry($entryName);
$data = new ZipFileData($entry, new \SplFileInfo(__FILE__));
$entry->setData($data);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
static::assertSame(
$zipFile->getEntryContents($entryName),
file_get_contents(__FILE__)
);
$zipFile->close();
}
}

116
tests/ZipInfoTest.php Normal file
View File

@@ -0,0 +1,116 @@
<?php
namespace PhpZip\Tests;
use PhpZip\Constants\ZipCompressionMethod;
use PhpZip\Constants\ZipEncryptionMethod;
use PhpZip\Constants\ZipPlatform;
use PhpZip\Exception\ZipEntryNotFoundException;
use PhpZip\Exception\ZipException;
use PhpZip\Model\ZipInfo;
use PhpZip\ZipFile;
/**
* Testing the {@see ZipInfo} class.
*
* {@see ZipInfo} is {@deprecated}. Use the {@see ZipEntry} class.
*
* @internal
*
* @small
*/
final class ZipInfoTest extends ZipTestCase
{
public function testZipAllInfo()
{
$zipFile = new ZipFile();
$zipFile['entry'] = 'contents';
$zipFile['entry 2'] = 'contents';
$zipAllInfo = $zipFile->getAllInfo();
$zipFile->close();
self::assertCount(2, $zipAllInfo);
self::assertContainsOnlyInstancesOf(ZipInfo::class, $zipAllInfo);
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testZipEntryInfo()
{
$zipFile = new ZipFile();
$zipFile['entry'] = 'contents';
$zipFile['entry 2'] = 'contents';
$zipInfo = $zipFile->getEntryInfo('entry');
$zipFile->close();
self::assertInstanceOf(ZipInfo::class, $zipInfo);
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testZipInfoEntryNotFound()
{
$this->setExpectedException(
ZipEntryNotFoundException::class,
'Zip Entry "unknown.name" was not found in the archive.'
);
$zipFile = new ZipFile();
$zipFile->getEntryInfo('unknown.name');
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testZipInfo()
{
$zipFile = new ZipFile();
$zipFile->openFile(__DIR__ . '/resources/Advanced-v1.0.0.epub');
$entryName = 'META-INF/container.xml';
$zipEntry = $zipFile->getEntry($entryName);
$zipInfo = $zipFile->getEntryInfo($entryName);
$zipFile->close();
self::assertSame($zipInfo->getName(), $zipEntry->getName());
self::assertSame($zipInfo->isFolder(), $zipEntry->isDirectory());
self::assertSame($zipInfo->getSize(), $zipEntry->getUncompressedSize());
self::assertSame($zipInfo->getCompressedSize(), $zipEntry->getCompressedSize());
self::assertSame($zipInfo->getMtime(), $zipEntry->getMTime()->getTimestamp());
self::assertSame(
$zipInfo->getCtime(),
$zipEntry->getCTime() !== null ? $zipEntry->getCTime()->getTimestamp() : null
);
self::assertSame(
$zipInfo->getAtime(),
$zipEntry->getATime() !== null ? $zipEntry->getATime()->getTimestamp() : null
);
self::assertNotEmpty($zipInfo->getAttributes());
self::assertSame($zipInfo->isEncrypted(), $zipEntry->isEncrypted());
self::assertSame($zipInfo->getComment(), $zipEntry->getComment());
self::assertSame($zipInfo->getCrc(), $zipEntry->getCrc());
self::assertSame(
$zipInfo->getMethod(),
ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
);
self::assertSame(
$zipInfo->getMethodName(),
ZipCompressionMethod::getCompressionMethodName($zipEntry->getCompressionMethod())
);
self::assertSame(
$zipInfo->getEncryptionMethodName(),
ZipEncryptionMethod::getEncryptionMethodName($zipEntry->getEncryptionMethod())
);
self::assertSame($zipInfo->getPlatform(), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
self::assertSame(ZipInfo::getPlatformName($zipEntry), ZipPlatform::getPlatformName($zipEntry->getExtractedOS()));
self::assertSame($zipInfo->getVersion(), $zipEntry->getExtractVersion());
self::assertNull($zipInfo->getEncryptionMethod());
self::assertSame($zipInfo->getCompressionLevel(), $zipEntry->getCompressionLevel());
self::assertSame($zipInfo->getCompressionMethod(), $zipEntry->getCompressionMethod());
self::assertNotEmpty($zipInfo->toArray());
}
}