mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-30 10:19:51 +02:00
Compare commits
12 Commits
3.3.0-alph
...
3.3.1
Author | SHA1 | Date | |
---|---|---|---|
|
52ce79d4d2 | ||
|
decf4f5095 | ||
|
70d8b8200b | ||
|
8fdc21eece | ||
|
74c0a49057 | ||
|
ae6d47f47a | ||
|
b3b676e3af | ||
|
f3d769739b | ||
|
074443dbc4 | ||
|
cbb693213e | ||
|
820c63c30f | ||
|
2235de6b35 |
@@ -24,7 +24,7 @@
|
|||||||
"php": "^5.5.9 || ^7.0",
|
"php": "^5.5.9 || ^7.0",
|
||||||
"ext-zlib": "*",
|
"ext-zlib": "*",
|
||||||
"psr/http-message": "^1.0",
|
"psr/http-message": "^1.0",
|
||||||
"paragonie/random_compat": ">=1 <9.99",
|
"paragonie/random_compat": "*",
|
||||||
"symfony/finder": "^3.0|^4.0|^5.0"
|
"symfony/finder": "^3.0|^4.0|^5.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
@@ -4,6 +4,7 @@ namespace PhpZip\Model\Data;
|
|||||||
|
|
||||||
use PhpZip\Exception\ZipException;
|
use PhpZip\Exception\ZipException;
|
||||||
use PhpZip\Model\ZipData;
|
use PhpZip\Model\ZipData;
|
||||||
|
use PhpZip\Model\ZipEntry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ZipFileData.
|
* Class ZipFileData.
|
||||||
@@ -16,11 +17,12 @@ class ZipFileData implements ZipData
|
|||||||
/**
|
/**
|
||||||
* ZipStringData constructor.
|
* ZipStringData constructor.
|
||||||
*
|
*
|
||||||
|
* @param ZipEntry $zipEntry
|
||||||
* @param \SplFileInfo $fileInfo
|
* @param \SplFileInfo $fileInfo
|
||||||
*
|
*
|
||||||
* @throws ZipException
|
* @throws ZipException
|
||||||
*/
|
*/
|
||||||
public function __construct(\SplFileInfo $fileInfo)
|
public function __construct(ZipEntry $zipEntry, \SplFileInfo $fileInfo)
|
||||||
{
|
{
|
||||||
if (!$fileInfo->isFile()) {
|
if (!$fileInfo->isFile()) {
|
||||||
throw new ZipException('$fileInfo is not a file.');
|
throw new ZipException('$fileInfo is not a file.');
|
||||||
@@ -31,6 +33,7 @@ class ZipFileData implements ZipData
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->file = $fileInfo;
|
$this->file = $fileInfo;
|
||||||
|
$zipEntry->setUncompressedSize($fileInfo->getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -142,7 +142,9 @@ class ZipFile implements ZipFileInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!($handle = fopen('php://temp', 'r+b'))) {
|
if (!($handle = fopen('php://temp', 'r+b'))) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
throw new ZipException("Can't open temp stream.");
|
throw new ZipException("Can't open temp stream.");
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
fwrite($handle, $data);
|
fwrite($handle, $data);
|
||||||
rewind($handle);
|
rewind($handle);
|
||||||
@@ -459,7 +461,9 @@ class ZipFile implements ZipFileInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!mkdir($dir, $dirMode, true) && !is_dir($dir)) {
|
if (!mkdir($dir, $dirMode, true) && !is_dir($dir)) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
chmod($dir, $dirMode);
|
chmod($dir, $dirMode);
|
||||||
}
|
}
|
||||||
@@ -495,6 +499,7 @@ class ZipFile implements ZipFileInterface
|
|||||||
|
|
||||||
/** @noinspection PhpUsageOfSilenceOperatorInspection */
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */
|
||||||
if (!($handle = @fopen($file, 'w+b'))) {
|
if (!($handle = @fopen($file, 'w+b'))) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
throw new ZipException(
|
throw new ZipException(
|
||||||
sprintf(
|
sprintf(
|
||||||
'Cannot extract zip entry %s. File %s cannot open for write.',
|
'Cannot extract zip entry %s. File %s cannot open for write.',
|
||||||
@@ -502,6 +507,7 @@ class ZipFile implements ZipFileInterface
|
|||||||
$file
|
$file
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -705,10 +711,9 @@ class ZipFile implements ZipFileInterface
|
|||||||
ZipCompressionMethod::DEFLATED;
|
ZipCompressionMethod::DEFLATED;
|
||||||
}
|
}
|
||||||
|
|
||||||
$zipEntry->setUncompressedSize($file->getSize());
|
|
||||||
$zipEntry->setCompressionMethod($compressionMethod);
|
$zipEntry->setCompressionMethod($compressionMethod);
|
||||||
|
|
||||||
$zipData = new ZipFileData($file);
|
$zipData = new ZipFileData($zipEntry, $file);
|
||||||
} elseif ($file->isDir()) {
|
} elseif ($file->isDir()) {
|
||||||
$zipEntry->setCompressionMethod(ZipCompressionMethod::STORED);
|
$zipEntry->setCompressionMethod(ZipCompressionMethod::STORED);
|
||||||
$zipEntry->setUncompressedSize(0);
|
$zipEntry->setUncompressedSize(0);
|
||||||
|
@@ -1571,9 +1571,10 @@ class ZipEntryTest extends TestCase
|
|||||||
public function testClone()
|
public function testClone()
|
||||||
{
|
{
|
||||||
$newUnixExtra = new NewUnixExtraField();
|
$newUnixExtra = new NewUnixExtraField();
|
||||||
$zipData = new ZipFileData(new \SplFileInfo(__FILE__));
|
|
||||||
|
|
||||||
$zipEntry = new ZipEntry('entry');
|
$zipEntry = new ZipEntry('entry');
|
||||||
|
$zipData = new ZipFileData($zipEntry, new \SplFileInfo(__FILE__));
|
||||||
|
|
||||||
$zipEntry->addExtraField($newUnixExtra);
|
$zipEntry->addExtraField($newUnixExtra);
|
||||||
$zipEntry->setData($zipData);
|
$zipEntry->setData($zipData);
|
||||||
|
|
||||||
|
@@ -10,6 +10,7 @@ use PhpZip\Exception\InvalidArgumentException;
|
|||||||
use PhpZip\Exception\ZipEntryNotFoundException;
|
use PhpZip\Exception\ZipEntryNotFoundException;
|
||||||
use PhpZip\Exception\ZipException;
|
use PhpZip\Exception\ZipException;
|
||||||
use PhpZip\Exception\ZipUnsupportMethodException;
|
use PhpZip\Exception\ZipUnsupportMethodException;
|
||||||
|
use PhpZip\Model\Data\ZipFileData;
|
||||||
use PhpZip\Model\ZipEntry;
|
use PhpZip\Model\ZipEntry;
|
||||||
use PhpZip\Model\ZipInfo;
|
use PhpZip\Model\ZipInfo;
|
||||||
use PhpZip\Util\FilesUtil;
|
use PhpZip\Util\FilesUtil;
|
||||||
@@ -2458,4 +2459,56 @@ class ZipFileTest extends ZipTestCase
|
|||||||
}
|
}
|
||||||
$zipFile->close();
|
$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
116
tests/ZipInfoTest.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user