mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-24 07:52:48 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
464050ff85 | ||
|
015166d165 | ||
|
47d308605e | ||
|
951433d0b7 | ||
|
9370f353c6 | ||
|
ac20d6fbf3 | ||
|
4c6f27c269 |
@@ -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){
|
||||||
|
@@ -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);
|
||||||
}
|
}
|
||||||
|
@@ -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'))) {
|
||||||
|
@@ -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] === '/') {
|
||||||
|
@@ -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();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user