1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-24 07:52:48 +02:00

7 Commits
2.0.1 ... 2.0.3

Author SHA1 Message Date
Ne-Lexa
464050ff85 Merge branch 'release/2.0.3' 2016-09-26 22:31:30 +03:00
Ne-Lexa
015166d165 Fix support entry name '0' 2016-09-26 22:30:30 +03:00
Ne-Lexa
47d308605e Correcting typos in the README 2016-09-26 18:33:05 +03:00
Ne-Lexa
951433d0b7 Merge branch 'release/2.0.2' 2016-09-26 18:07:38 +03:00
Ne-Lexa
9370f353c6 Merge tag '2.0.2' into develop
Tagging version 2.0.2 2.0.2
2016-09-26 18:07:38 +03:00
Ne-Lexa
ac20d6fbf3 Optimize pack and unpack long fro PHP >= 5.6.3 2016-09-26 18:04:52 +03:00
Ne-Lexa
4c6f27c269 Merge tag '2.0.1' into develop
Tagging version 2.0.1 2.0.1
2016-09-26 16:56:46 +03:00
5 changed files with 44 additions and 24 deletions

View File

@@ -362,7 +362,7 @@ $zipOutputFile->saveAsFile($filename);
``` ```
Save archive to a stream. Save archive to a stream.
```php ```php
$handle = fopen($filename, 'w+b); $handle = fopen($filename, 'w+b');
$autoCloseResource = true; $autoCloseResource = true;
$zipOutputFile->saveAsStream($handle, $autoCloseResource); $zipOutputFile->saveAsStream($handle, $autoCloseResource);
if(!$autoCloseResource){ if(!$autoCloseResource){

View File

@@ -18,7 +18,9 @@ class PackUtil
*/ */
public static function packLongLE($longValue) public static function packLongLE($longValue)
{ {
// TODO test if (version_compare(PHP_VERSION, '5.6.3') >= 0) {return pack("P", $longValue);} if (version_compare(PHP_VERSION, '5.6.3') >= 0) {
return pack("P", $longValue);
}
$left = 0xffffffff00000000; $left = 0xffffffff00000000;
$right = 0x00000000ffffffff; $right = 0x00000000ffffffff;
@@ -36,7 +38,9 @@ class PackUtil
*/ */
public static function unpackLongLE($value) public static function unpackLongLE($value)
{ {
// TODO test if (version_compare(PHP_VERSION, '5.6.3') >= 0){ return current(unpack('P', $value)); } if (version_compare(PHP_VERSION, '5.6.3') >= 0) {
return current(unpack('P', $value));
}
$unpack = unpack('Va/Vb', $value); $unpack = unpack('Va/Vb', $value);
return $unpack['a'] + ($unpack['b'] << 32); return $unpack['a'] + ($unpack['b'] << 32);
} }

View File

@@ -401,7 +401,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
*/ */
public static function openFromString($data) public static function openFromString($data)
{ {
if (empty($data)) { if (null === $data || strlen($data) === 0) {
throw new IllegalArgumentException("Data not available"); throw new IllegalArgumentException("Data not available");
} }
if (!($handle = fopen('php://temp', 'r+b'))) { if (!($handle = fopen('php://temp', 'r+b'))) {

View File

@@ -269,7 +269,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
*/ */
public function setComment($comment) public function setComment($comment)
{ {
if (null !== $comment && !empty($comment)) { if (null !== $comment && strlen($comment) !== 0) {
$comment = (string)$comment; $comment = (string)$comment;
$length = strlen($comment); $length = strlen($comment);
if (0x0000 > $length || $length > 0xffff) { if (0x0000 > $length || $length > 0xffff) {
@@ -292,10 +292,10 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
public function addFromString($entryName, $data, $compressionMethod = ZipEntry::METHOD_DEFLATED) public function addFromString($entryName, $data, $compressionMethod = ZipEntry::METHOD_DEFLATED)
{ {
$entryName = (string)$entryName; $entryName = (string)$entryName;
if ($data === null) { if ($data === null || strlen($data) === 0) {
throw new IllegalArgumentException("data is null"); throw new IllegalArgumentException("Data is empty");
} }
if (empty($entryName)) { if ($entryName === null || strlen($entryName) === 0) {
throw new IllegalArgumentException("Incorrect entry name " . $entryName); throw new IllegalArgumentException("Incorrect entry name " . $entryName);
} }
$this->validateCompressionMethod($compressionMethod); $this->validateCompressionMethod($compressionMethod);
@@ -343,7 +343,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
) )
{ {
$inputDir = (string)$inputDir; $inputDir = (string)$inputDir;
if (empty($inputDir)) { if ($inputDir === null || strlen($inputDir) === 0) {
throw new IllegalArgumentException('Input dir empty'); throw new IllegalArgumentException('Input dir empty');
} }
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
@@ -367,8 +367,12 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
foreach ($files as $file) { foreach ($files as $file) {
$filename = str_replace($inputDir, $moveToPath, $file); $filename = str_replace($inputDir, $moveToPath, $file);
$filename = ltrim($filename, '/'); $filename = ltrim($filename, '/');
is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename); if(is_dir($file)){
is_file($file) && $this->addFromFile($file, $filename, $compressionMethod); FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
}
elseif(is_file($file)){
$this->addFromFile($file, $filename, $compressionMethod);
}
} }
return $this->count() > $count; return $this->count() > $count;
} }
@@ -392,7 +396,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
public function addEmptyDir($dirName) public function addEmptyDir($dirName)
{ {
$dirName = (string)$dirName; $dirName = (string)$dirName;
if (empty($dirName)) { if (strlen($dirName) === 0) {
throw new IllegalArgumentException("dirName null or not string"); throw new IllegalArgumentException("dirName null or not string");
} }
$dirName = rtrim($dirName, '/') . '/'; $dirName = rtrim($dirName, '/') . '/';
@@ -447,7 +451,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
throw new IllegalArgumentException("stream is not resource"); throw new IllegalArgumentException("stream is not resource");
} }
$entryName = (string)$entryName; $entryName = (string)$entryName;
if (empty($entryName)) { if (strlen($entryName) === 0) {
throw new IllegalArgumentException("Incorrect entry name " . $entryName); throw new IllegalArgumentException("Incorrect entry name " . $entryName);
} }
$this->validateCompressionMethod($compressionMethod); $this->validateCompressionMethod($compressionMethod);
@@ -486,7 +490,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
if (!is_dir($inputDir)) { if (!is_dir($inputDir)) {
throw new IllegalArgumentException('Directory ' . $inputDir . ' can\'t exists'); throw new IllegalArgumentException('Directory ' . $inputDir . ' can\'t exists');
} }
if (null === $globPattern || !is_string($globPattern)) { if (null === $globPattern || strlen($globPattern) === 0) {
throw new IllegalArgumentException("globPattern null"); throw new IllegalArgumentException("globPattern null");
} }
if (empty($globPattern)) { if (empty($globPattern)) {
@@ -514,8 +518,12 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
foreach ($filesFound as $file) { foreach ($filesFound as $file) {
$filename = str_replace($inputDir, $moveToPath, $file); $filename = str_replace($inputDir, $moveToPath, $file);
$filename = ltrim($filename, '/'); $filename = ltrim($filename, '/');
is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename); if(is_dir($file)){
is_file($file) && $this->addFromFile($file, $filename, $compressionMethod); FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
}
elseif(is_file($file)){
$this->addFromFile($file, $filename, $compressionMethod);
}
} }
return $this->count() > $count; return $this->count() > $count;
} }
@@ -571,8 +579,12 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
foreach ($files as $file) { foreach ($files as $file) {
$filename = str_replace($inputDir, $moveToPath, $file); $filename = str_replace($inputDir, $moveToPath, $file);
$filename = ltrim($filename, '/'); $filename = ltrim($filename, '/');
is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename); if(is_dir($file)){
is_file($file) && $this->addFromFile($file, $filename, $compressionMethod); FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
}
elseif(is_file($file)){
$this->addFromFile($file, $filename, $compressionMethod);
}
} }
return $this->count() > $count; return $this->count() > $count;
} }
@@ -1268,7 +1280,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
public function outputAsAttachment($outputFilename, $mimeType = null) public function outputAsAttachment($outputFilename, $mimeType = null)
{ {
$outputFilename = (string)$outputFilename; $outputFilename = (string)$outputFilename;
if (empty($outputFilename)) { if (strlen($outputFilename) === 0) {
throw new IllegalArgumentException("Output filename is empty."); throw new IllegalArgumentException("Output filename is empty.");
} }
if (empty($mimeType) || !is_string($mimeType)) { if (empty($mimeType) || !is_string($mimeType)) {
@@ -1360,7 +1372,8 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
*/ */
public function offsetSet($entryName, $uncompressedDataContent) public function offsetSet($entryName, $uncompressedDataContent)
{ {
if (empty($entryName)) { $entryName = (string)$entryName;
if (strlen($entryName) === 0) {
throw new IllegalArgumentException('Entry name empty'); throw new IllegalArgumentException('Entry name empty');
} }
if ($entryName[strlen($entryName) - 1] === '/') { if ($entryName[strlen($entryName) - 1] === '/') {

View File

@@ -848,7 +848,7 @@ class ZipTest extends ZipTestCase
'data' => CryptoUtil::randomBytes(255), 'data' => CryptoUtil::randomBytes(255),
'password' => CryptoUtil::randomBytes(255), 'password' => CryptoUtil::randomBytes(255),
'encryption_method' => ZipEntry::ENCRYPTION_METHOD_WINZIP_AES, 'encryption_method' => ZipEntry::ENCRYPTION_METHOD_WINZIP_AES,
'compression_method' => ZipEntry::METHOD_BZIP2, 'compression_method' => extension_loaded("bz2") ? ZipEntry::METHOD_BZIP2 : ZipEntry::METHOD_STORED,
], ],
'Not password.dat' => [ 'Not password.dat' => [
'data' => CryptoUtil::randomBytes(255), 'data' => CryptoUtil::randomBytes(255),
@@ -1022,6 +1022,9 @@ class ZipTest extends ZipTestCase
$zipFile->close(); $zipFile->close();
} }
/**
* Test zip alignment.
*/
public function testZipAlign() public function testZipAlign()
{ {
$zipOutputFile = ZipOutputFile::create(); $zipOutputFile = ZipOutputFile::create();
@@ -1042,7 +1045,7 @@ class ZipTest extends ZipTestCase
if($result === null) return; // zip align not installed if($result === null) return; // zip align not installed
// check not zip align // check not zip align
self::assertFalse($result, false); self::assertFalse($result);
$zipFile = ZipFile::openFromFile($this->outputFilename); $zipFile = ZipFile::openFromFile($this->outputFilename);
$zipOutputFile = ZipOutputFile::openFromZipFile($zipFile); $zipOutputFile = ZipOutputFile::openFromZipFile($zipFile);
@@ -1062,6 +1065,7 @@ class ZipTest extends ZipTestCase
/** /**
* Test support ZIP64 ext (slow test - normal). * Test support ZIP64 ext (slow test - normal).
* Create > 65535 files in archive and open and extract to /dev/null.
*/ */
public function testCreateAndOpenZip64Ext() public function testCreateAndOpenZip64Ext()
{ {
@@ -1078,8 +1082,7 @@ class ZipTest extends ZipTestCase
$zipFile = ZipFile::openFromFile($this->outputFilename); $zipFile = ZipFile::openFromFile($this->outputFilename);
self::assertEquals($zipFile->count(), $countFiles); self::assertEquals($zipFile->count(), $countFiles);
foreach ($zipFile->getListFiles() as $entry) { foreach ($zipFile as $entry => $content) {
$zipFile->getEntryContent($entry);
} }
$zipFile->close(); $zipFile->close();
} }