mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-08-15 11:44:56 +02:00
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d8e40ee3f1 | ||
|
58e9f4bf73 | ||
|
39f8616336 | ||
|
8d140cc1a1 | ||
|
11a7c16f1b | ||
|
f2ffdae0c2 | ||
|
676eca7f87 | ||
|
2c402157ca | ||
|
464050ff85 | ||
|
294e3d54ef | ||
|
015166d165 | ||
|
47d308605e | ||
|
951433d0b7 | ||
|
9370f353c6 | ||
|
ac20d6fbf3 | ||
|
2729d8cbec | ||
|
4c6f27c269 | ||
|
f0d90da75c | ||
|
cf7f98b7c9 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
/vendor/
|
||||
/vendor
|
||||
*.iml
|
||||
/.idea/
|
||||
/.idea
|
||||
/composer.lock
|
5
CHANGELOG.md
Normal file
5
CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## 2.2.0 (2017-03-02)
|
||||
|
||||
Features:
|
||||
- create output object `ZipOutputFile` from `ZipFile` in method `ZipFile::edit()`.
|
||||
- create output object `ZipOutputFile` from filename in static method `ZipOutputFile::openFromFile(string $filename)`.
|
42
README.md
42
README.md
@@ -2,14 +2,16 @@
|
||||
================
|
||||
`PhpZip` - is to create, update, opening and unpacking ZIP archives in pure PHP.
|
||||
|
||||
The library supports `ZIP64`, `Traditional PKWARE Encryption` and `WinZIP AES Encryption`.
|
||||
The library supports `ZIP64`, `zipalign`, `Traditional PKWARE Encryption` and `WinZIP AES Encryption`.
|
||||
|
||||
The library does not require extension `php-xml` and class `ZipArchive`.
|
||||
ZIP64 extensions are automatically and transparently activated when reading or writing ZIP files of more than 4 GB size.
|
||||
|
||||
The library does not require extension `php-zip` and class `ZipArchive`.
|
||||
|
||||
Requirements
|
||||
------------
|
||||
- `PHP` >= 5.4 (64 bit)
|
||||
- Php-extension `mbstring`
|
||||
- PHP-extension `mbstring`
|
||||
- Optional php-extension `bzip2` for BZIP2 compression.
|
||||
- Optional php-extension `openssl` or `mcrypt` for `WinZip Aes Encryption` support.
|
||||
|
||||
@@ -95,7 +97,7 @@ Get entry info.
|
||||
```php
|
||||
$zipInfo = $zipFile->getEntryInfo('file.txt');
|
||||
echo $zipInfo . PHP_EOL;
|
||||
// ZipInfo {Path="file.txt", Size=9.77KB, Compressed size=2.04KB, Modified time=2016-09-24T19:25:10+03:00, Crc=0x4b5ab5c7, Method="Deflate", Platform="UNIX", Version=20}
|
||||
// ZipInfo {Path="file.txt", Size=9.77KB, Compressed size=2.04KB, Modified time=2016-09-24T19:25:10+03:00, Crc=0x4b5ab5c7, Method="Deflate", Attributes="-rw-r--r--", Platform="UNIX", Version=20}
|
||||
print_r($zipInfo);
|
||||
//PhpZip\Model\ZipInfo Object
|
||||
//(
|
||||
@@ -112,6 +114,7 @@ print_r($zipInfo);
|
||||
// [method:PhpZip\Model\ZipInfo:private] => Deflate
|
||||
// [platform:PhpZip\Model\ZipInfo:private] => UNIX
|
||||
// [version:PhpZip\Model\ZipInfo:private] => 20
|
||||
// [attributes:PhpZip\Model\ZipInfo:private] => -rw-r--r--
|
||||
//)
|
||||
```
|
||||
Get info for all entries.
|
||||
@@ -146,6 +149,10 @@ Get entry content.
|
||||
```php
|
||||
$data = $zipFile->getEntryContent($entryName);
|
||||
```
|
||||
Edit zip archive
|
||||
```php
|
||||
$zipOutputFile = $zipFile->edit();
|
||||
```
|
||||
Close zip archive.
|
||||
```php
|
||||
$zipFile->close();
|
||||
@@ -159,6 +166,11 @@ $zipOutputFile = \PhpZip\ZipOutputFile::create();
|
||||
```
|
||||
Open zip file from update.
|
||||
```php
|
||||
$filename = "file.zip";
|
||||
$zipOutputFile = \PhpZip\ZipOutputFile::openFromFile($filename);
|
||||
```
|
||||
or
|
||||
```php
|
||||
// initial ZipFile
|
||||
$zipFile = \PhpZip\ZipFile::openFromFile($filename);
|
||||
|
||||
@@ -166,23 +178,28 @@ $zipFile = \PhpZip\ZipFile::openFromFile($filename);
|
||||
$zipOutputFile = new \PhpZip\ZipOutputFile($zipFile);
|
||||
// or
|
||||
$zipOutputFile = \PhpZip\ZipOutputFile::openFromZipFile($zipFile);
|
||||
// or
|
||||
$zipOutputFile = $zipFile->edit();
|
||||
```
|
||||
Add entry from file.
|
||||
```php
|
||||
$zipOutputFile->addFromFile($filename); // $entryName == basename($filename);
|
||||
$zipOutputFile->addFromFile($filename, $entryName);
|
||||
$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_DEFLATED);
|
||||
$zipOutputFile->addFromFile($filename, $entryName, ZipEntry::METHOD_STORED); // no compress
|
||||
$zipOutputFile->addFromFile($filename, null, ZipEntry::METHOD_BZIP2); // $entryName == basename($filename);
|
||||
```
|
||||
Add entry from string data.
|
||||
```php
|
||||
$zipOutputFile->addFromString($entryName, $data)
|
||||
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED)
|
||||
$zipOutputFile->addFromString($entryName, $data);
|
||||
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_DEFLATED);
|
||||
$zipOutputFile->addFromString($entryName, $data, ZipEntry::METHOD_STORED); // no compress
|
||||
```
|
||||
Add entry from stream.
|
||||
```php
|
||||
$zipOutputFile->addFromStream($stream, $entryName)
|
||||
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED)
|
||||
$zipOutputFile->addFromStream($stream, $entryName);
|
||||
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_DEFLATED);
|
||||
$zipOutputFile->addFromStream($stream, $entryName, ZipEntry::METHOD_STORED); // no compress
|
||||
```
|
||||
Add empty dir
|
||||
```php
|
||||
@@ -362,7 +379,7 @@ $zipOutputFile->saveAsFile($filename);
|
||||
```
|
||||
Save archive to a stream.
|
||||
```php
|
||||
$handle = fopen($filename, 'w+b);
|
||||
$handle = fopen($filename, 'w+b');
|
||||
$autoCloseResource = true;
|
||||
$zipOutputFile->saveAsStream($handle, $autoCloseResource);
|
||||
if(!$autoCloseResource){
|
||||
@@ -415,6 +432,11 @@ while ($iterator->valid())
|
||||
$iterator->next();
|
||||
}
|
||||
```
|
||||
Set zip alignment (alternate program `zipalign`).
|
||||
```php
|
||||
// before save or output
|
||||
$zipOutputFile->setAlign(4); // alternative cmd: zipalign -f -v 4 filename.zip
|
||||
```
|
||||
Close zip archive.
|
||||
```php
|
||||
$zipOutputFile->close();
|
||||
@@ -438,7 +460,7 @@ $zipOutputFile->close(); // close output file, release all streams
|
||||
$zipFile = \PhpZip\ZipFile::openFromFile($outputFilename); // open zip archive from file
|
||||
$zipFile->extractTo($outputDirExtract); // extract files to dir
|
||||
|
||||
$zipOutputFile = \PhpZip\ZipOutputFile::openFromZipFile($zipFile); // create zip output archive for update
|
||||
$zipOutputFile = $zipFile->edit(); // create zip output archive for update
|
||||
$zipOutputFile->deleteFromRegex('~^\.~'); // delete all hidden (Unix) files
|
||||
$zipOutputFile->addFromString('dir/file.txt', 'Test file'); // add files from string contents
|
||||
$zipOutputFile->saveAsFile($outputFilename); // update zip file
|
||||
|
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"name": "nelexa/zip",
|
||||
"description": "Zip files CRUD. Open, create, update, extract and get info tool. Support read and write encrypted archives. Support ZIP64 ext. Alternative ZipArchive. It does not require php-zip extension.",
|
||||
"description": "Zip files CRUD. Open, create, update, extract and get info tool. Supports appending to existing ZIP files, WinZip AES encryption, Traditional PKWARE Encryption, ZipAlign tool, BZIP2 compression, external file attributes and ZIP64 extensions. Alternative ZipArchive. It does not require php-zip extension.",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"zip",
|
||||
"archive",
|
||||
"extract",
|
||||
"winzip"
|
||||
"winzip",
|
||||
"zipalign"
|
||||
],
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.8"
|
||||
|
@@ -105,9 +105,26 @@ class TraditionalPkwareEncryptionEngine
|
||||
private function updateKeys($charAt)
|
||||
{
|
||||
$this->keys[0] = self::crc32($this->keys[0], $charAt);
|
||||
$this->keys[1] = ($this->keys[1] + ($this->keys[0] & 0xff)) & 4294967295;
|
||||
$this->keys[1] = ($this->keys[1] * 134775813 + 1) & 4294967295;
|
||||
$this->keys[2] = self::crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff);
|
||||
$this->keys[1] = $this->keys[1] + ($this->keys[0] & 0xff);
|
||||
$this->keys[1] = self::toInt($this->keys[1] * 134775813 + 1);
|
||||
$this->keys[2] = self::toInt(self::crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to int
|
||||
*
|
||||
* @param $i
|
||||
* @return int
|
||||
*/
|
||||
private static function toInt($i)
|
||||
{
|
||||
$i = (int)($i & 0xffffffff);
|
||||
if ($i > 2147483647) {
|
||||
return -(-$i & 0xffffffff);
|
||||
} elseif ($i < -2147483648) {
|
||||
return $i & -2147483648;
|
||||
}
|
||||
return $i;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -88,16 +88,20 @@ class WinZipAesEngine
|
||||
throw new ZipCryptoException("Expected end of file after WinZip AES authentication code!");
|
||||
}
|
||||
|
||||
do {
|
||||
assert($this->entry->getPassword() !== null);
|
||||
assert(self::AES_BLOCK_SIZE_BITS <= $keyStrengthBits);
|
||||
$password = $this->entry->getPassword();
|
||||
assert($password !== null);
|
||||
assert(self::AES_BLOCK_SIZE_BITS <= $keyStrengthBits);
|
||||
|
||||
// WinZip 99-character limit
|
||||
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
$password = substr($password, 0, 99);
|
||||
do {
|
||||
// Here comes the strange part about WinZip AES encryption:
|
||||
// Its unorthodox use of the Password-Based Key Derivation
|
||||
// Function 2 (PBKDF2) of PKCS #5 V2.0 alias RFC 2898.
|
||||
// Yes, the password verifier is only a 16 bit value.
|
||||
// So we must use the MAC for password verification, too.
|
||||
$keyParam = hash_pbkdf2("sha1", $this->entry->getPassword(), $salt, self::ITERATION_COUNT, (2 * $keyStrengthBits + self::PWD_VERIFIER_BITS) / 8, true);
|
||||
$keyParam = hash_pbkdf2("sha1", $password, $salt, self::ITERATION_COUNT, (2 * $keyStrengthBits + self::PWD_VERIFIER_BITS) / 8, true);
|
||||
$ctrIvSize = self::AES_BLOCK_SIZE_BITS / 8;
|
||||
$iv = str_repeat(chr(0), $ctrIvSize);
|
||||
|
||||
@@ -202,6 +206,10 @@ class WinZipAesEngine
|
||||
$password = $this->entry->getPassword();
|
||||
assert($password !== null);
|
||||
|
||||
// WinZip 99-character limit
|
||||
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
$password = substr($password, 0, 99);
|
||||
|
||||
$keyStrengthBytes = 32;
|
||||
$keyStrengthBits = $keyStrengthBytes * 8;
|
||||
|
||||
|
@@ -36,6 +36,31 @@ class ZipInfo
|
||||
const MADE_BY_OS_X = 19;
|
||||
const MADE_BY_UNKNOWN = 20;
|
||||
|
||||
const UNX_IFMT = 0170000; /* Unix file type mask */
|
||||
const UNX_IFREG = 0100000; /* Unix regular file */
|
||||
const UNX_IFSOCK = 0140000; /* Unix socket (BSD, not SysV or Amiga) */
|
||||
const UNX_IFLNK = 0120000; /* Unix symbolic link (not SysV, Amiga) */
|
||||
const UNX_IFBLK = 0060000; /* Unix block special (not Amiga) */
|
||||
const UNX_IFDIR = 0040000; /* Unix directory */
|
||||
const UNX_IFCHR = 0020000; /* Unix character special (not Amiga) */
|
||||
const UNX_IFIFO = 0010000; /* Unix fifo (BCC, not MSC or Amiga) */
|
||||
const UNX_ISUID = 04000; /* Unix set user id on execution */
|
||||
const UNX_ISGID = 02000; /* Unix set group id on execution */
|
||||
const UNX_ISVTX = 01000; /* Unix directory permissions control */
|
||||
const UNX_ENFMT = self::UNX_ISGID; /* Unix record locking enforcement flag */
|
||||
const UNX_IRWXU = 00700; /* Unix read, write, execute: owner */
|
||||
const UNX_IRUSR = 00400; /* Unix read permission: owner */
|
||||
const UNX_IWUSR = 00200; /* Unix write permission: owner */
|
||||
const UNX_IXUSR = 00100; /* Unix execute permission: owner */
|
||||
const UNX_IRWXG = 00070; /* Unix read, write, execute: group */
|
||||
const UNX_IRGRP = 00040; /* Unix read permission: group */
|
||||
const UNX_IWGRP = 00020; /* Unix write permission: group */
|
||||
const UNX_IXGRP = 00010; /* Unix execute permission: group */
|
||||
const UNX_IRWXO = 00007; /* Unix read, write, execute: other */
|
||||
const UNX_IROTH = 00004; /* Unix read permission: other */
|
||||
const UNX_IWOTH = 00002; /* Unix write permission: other */
|
||||
const UNX_IXOTH = 00001; /* Unix execute permission: other */
|
||||
|
||||
private static $valuesMadeBy = [
|
||||
self::MADE_BY_MS_DOS => 'FAT',
|
||||
self::MADE_BY_AMIGA => 'Amiga',
|
||||
@@ -150,6 +175,11 @@ class ZipInfo
|
||||
*/
|
||||
private $version;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $attributes;
|
||||
|
||||
/**
|
||||
* ZipInfo constructor.
|
||||
*
|
||||
@@ -183,6 +213,88 @@ class ZipInfo
|
||||
$this->method = self::getMethodName($entry);
|
||||
$this->platform = self::getPlatformName($entry);
|
||||
$this->version = $entry->getVersionNeededToExtract();
|
||||
|
||||
$attribs = str_repeat(" ", 12);
|
||||
$xattr = (($entry->getRawExternalAttributes() >> 16) & 0xFFFF);
|
||||
switch ($entry->getPlatform()) {
|
||||
case self::MADE_BY_MS_DOS:
|
||||
case self::MADE_BY_WINDOWS_NTFS:
|
||||
if ($entry->getPlatform() != self::MADE_BY_MS_DOS ||
|
||||
($xattr & 0700) !=
|
||||
(0400 |
|
||||
(!($entry->getRawExternalAttributes() & 1) << 7) |
|
||||
(($entry->getRawExternalAttributes() & 0x10) << 2))
|
||||
) {
|
||||
$xattr = $entry->getRawExternalAttributes() & 0xFF;
|
||||
$attribs = ".r.-... ";
|
||||
$attribs[2] = ($xattr & 0x01) ? '-' : 'w';
|
||||
$attribs[5] = ($xattr & 0x02) ? 'h' : '-';
|
||||
$attribs[6] = ($xattr & 0x04) ? 's' : '-';
|
||||
$attribs[4] = ($xattr & 0x20) ? 'a' : '-';
|
||||
if ($xattr & 0x10) {
|
||||
$attribs[0] = 'd';
|
||||
$attribs[3] = 'x';
|
||||
} else
|
||||
$attribs[0] = '-';
|
||||
if ($xattr & 0x08)
|
||||
$attribs[0] = 'V';
|
||||
else {
|
||||
$ext = strtolower(pathinfo($entry->getName(), PATHINFO_EXTENSION));
|
||||
if (in_array($ext, ["com", "exe", "btm", "cmd", "bat"])) {
|
||||
$attribs[3] = 'x';
|
||||
}
|
||||
}
|
||||
break;
|
||||
} /* else: fall through! */
|
||||
|
||||
default: /* assume Unix-like */
|
||||
switch ($xattr & self::UNX_IFMT) {
|
||||
case self::UNX_IFDIR:
|
||||
$attribs[0] = 'd';
|
||||
break;
|
||||
case self::UNX_IFREG:
|
||||
$attribs[0] = '-';
|
||||
break;
|
||||
case self::UNX_IFLNK:
|
||||
$attribs[0] = 'l';
|
||||
break;
|
||||
case self::UNX_IFBLK:
|
||||
$attribs[0] = 'b';
|
||||
break;
|
||||
case self::UNX_IFCHR:
|
||||
$attribs[0] = 'c';
|
||||
break;
|
||||
case self::UNX_IFIFO:
|
||||
$attribs[0] = 'p';
|
||||
break;
|
||||
case self::UNX_IFSOCK:
|
||||
$attribs[0] = 's';
|
||||
break;
|
||||
default:
|
||||
$attribs[0] = '?';
|
||||
break;
|
||||
}
|
||||
$attribs[1] = ($xattr & self::UNX_IRUSR) ? 'r' : '-';
|
||||
$attribs[4] = ($xattr & self::UNX_IRGRP) ? 'r' : '-';
|
||||
$attribs[7] = ($xattr & self::UNX_IROTH) ? 'r' : '-';
|
||||
$attribs[2] = ($xattr & self::UNX_IWUSR) ? 'w' : '-';
|
||||
$attribs[5] = ($xattr & self::UNX_IWGRP) ? 'w' : '-';
|
||||
$attribs[8] = ($xattr & self::UNX_IWOTH) ? 'w' : '-';
|
||||
|
||||
if ($xattr & self::UNX_IXUSR)
|
||||
$attribs[3] = ($xattr & self::UNX_ISUID) ? 's' : 'x';
|
||||
else
|
||||
$attribs[3] = ($xattr & self::UNX_ISUID) ? 'S' : '-'; /* S==undefined */
|
||||
if ($xattr & self::UNX_IXGRP)
|
||||
$attribs[6] = ($xattr & self::UNX_ISGID) ? 's' : 'x'; /* == UNX_ENFMT */
|
||||
else
|
||||
$attribs[6] = ($xattr & self::UNX_ISGID) ? 'S' : '-'; /* SunOS 4.1.x */
|
||||
if ($xattr & self::UNX_IXOTH)
|
||||
$attribs[9] = ($xattr & self::UNX_ISVTX) ? 't' : 'x'; /* "sticky bit" */
|
||||
else
|
||||
$attribs[9] = ($xattr & self::UNX_ISVTX) ? 'T' : '-'; /* T==undefined */
|
||||
}
|
||||
$this->attributes = trim($attribs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,6 +357,7 @@ class ZipInfo
|
||||
'modified' => $this->getMtime(),
|
||||
'created' => $this->getCtime(),
|
||||
'accessed' => $this->getAtime(),
|
||||
'attributes' => $this->getAttributes(),
|
||||
'encrypted' => $this->isEncrypted(),
|
||||
'comment' => $this->getComment(),
|
||||
'crc' => $this->getCrc(),
|
||||
@@ -358,6 +471,14 @@ class ZipInfo
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAttributes()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -375,6 +496,7 @@ class ZipInfo
|
||||
. (!empty($this->comment) ? 'Comment="' . $this->getComment() . '", ' : '')
|
||||
. (!empty($this->crc) ? 'Crc=0x' . dechex($this->getCrc()) . ', ' : '')
|
||||
. 'Method="' . $this->getMethod() . '", '
|
||||
. 'Attributes="' . $this->getAttributes() . '", '
|
||||
. 'Platform="' . $this->getPlatform() . '", '
|
||||
. 'Version=' . $this->getVersion()
|
||||
. '}';
|
||||
|
@@ -18,7 +18,9 @@ class PackUtil
|
||||
*/
|
||||
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;
|
||||
$right = 0x00000000ffffffff;
|
||||
@@ -36,7 +38,9 @@ class PackUtil
|
||||
*/
|
||||
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);
|
||||
return $unpack['a'] + ($unpack['b'] << 32);
|
||||
}
|
||||
|
@@ -157,6 +157,13 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
return $zipFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ZipOutputFile
|
||||
*/
|
||||
public function edit(){
|
||||
return ZipOutputFile::openFromZipFile($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check zip file signature
|
||||
*
|
||||
@@ -401,7 +408,7 @@ class ZipFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
*/
|
||||
public static function openFromString($data)
|
||||
{
|
||||
if (empty($data)) {
|
||||
if (null === $data || strlen($data) === 0) {
|
||||
throw new IllegalArgumentException("Data not available");
|
||||
}
|
||||
if (!($handle = fopen('php://temp', 'r+b'))) {
|
||||
|
@@ -103,6 +103,13 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
*/
|
||||
private $level = self::LEVEL_DEFAULT_COMPRESSION;
|
||||
|
||||
/**
|
||||
* ZipAlign setting
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $align;
|
||||
|
||||
/**
|
||||
* ZipOutputFile constructor.
|
||||
* @param ZipFile|null $zipFile
|
||||
@@ -145,6 +152,32 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
return new self($zipFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open zip file from update.
|
||||
*
|
||||
* @param string $filename
|
||||
* @return ZipOutputFile
|
||||
* @throws IllegalArgumentException
|
||||
* @see ZipOutputFile::__construct()
|
||||
*/
|
||||
public static function openFromFile($filename)
|
||||
{
|
||||
if (empty($filename)) {
|
||||
throw new IllegalArgumentException("Zip file is null");
|
||||
}
|
||||
return new self(ZipFile::openFromFile($filename));
|
||||
}
|
||||
|
||||
/**
|
||||
* Count zip entries.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return sizeof($this->entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list files.
|
||||
*
|
||||
@@ -262,7 +295,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
*/
|
||||
public function setComment($comment)
|
||||
{
|
||||
if (null !== $comment && !empty($comment)) {
|
||||
if (null !== $comment && strlen($comment) !== 0) {
|
||||
$comment = (string)$comment;
|
||||
$length = strlen($comment);
|
||||
if (0x0000 > $length || $length > 0xffff) {
|
||||
@@ -285,17 +318,20 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
public function addFromString($entryName, $data, $compressionMethod = ZipEntry::METHOD_DEFLATED)
|
||||
{
|
||||
$entryName = (string)$entryName;
|
||||
if ($data === null) {
|
||||
throw new IllegalArgumentException("data is null");
|
||||
if ($data === null || strlen($data) === 0) {
|
||||
throw new IllegalArgumentException("Data is empty");
|
||||
}
|
||||
if (empty($entryName)) {
|
||||
if ($entryName === null || strlen($entryName) === 0) {
|
||||
throw new IllegalArgumentException("Incorrect entry name " . $entryName);
|
||||
}
|
||||
$this->validateCompressionMethod($compressionMethod);
|
||||
|
||||
$externalAttributes = 0100644 << 16;
|
||||
|
||||
$entry = new ZipEntry($entryName);
|
||||
$entry->setMethod($compressionMethod);
|
||||
$entry->setTime(time());
|
||||
$entry->setExternalAttributes($externalAttributes);
|
||||
|
||||
$this->entries[$entryName] = new ZipOutputStringEntry($data, $entry);
|
||||
}
|
||||
@@ -336,7 +372,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
)
|
||||
{
|
||||
$inputDir = (string)$inputDir;
|
||||
if (empty($inputDir)) {
|
||||
if ($inputDir === null || strlen($inputDir) === 0) {
|
||||
throw new IllegalArgumentException('Input dir empty');
|
||||
}
|
||||
if (!is_dir($inputDir)) {
|
||||
@@ -360,22 +396,15 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
foreach ($files as $file) {
|
||||
$filename = str_replace($inputDir, $moveToPath, $file);
|
||||
$filename = ltrim($filename, '/');
|
||||
is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
|
||||
is_file($file) && $this->addFromFile($file, $filename, $compressionMethod);
|
||||
if (is_dir($file)) {
|
||||
FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
|
||||
} elseif (is_file($file)) {
|
||||
$this->addFromFile($file, $filename, $compressionMethod);
|
||||
}
|
||||
}
|
||||
return $this->count() > $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count zip entries.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return sizeof($this->entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an empty directory in the zip archive.
|
||||
*
|
||||
@@ -385,17 +414,20 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
public function addEmptyDir($dirName)
|
||||
{
|
||||
$dirName = (string)$dirName;
|
||||
if (empty($dirName)) {
|
||||
if (strlen($dirName) === 0) {
|
||||
throw new IllegalArgumentException("dirName null or not string");
|
||||
}
|
||||
$dirName = rtrim($dirName, '/') . '/';
|
||||
if (!isset($this->entries[$dirName])) {
|
||||
$externalAttributes = 040755 << 16;
|
||||
|
||||
$entry = new ZipEntry($dirName);
|
||||
$entry->setTime(time());
|
||||
$entry->setMethod(ZipEntry::METHOD_STORED);
|
||||
$entry->setSize(0);
|
||||
$entry->setCompressedSize(0);
|
||||
$entry->setCrc(0);
|
||||
$entry->setExternalAttributes($externalAttributes);
|
||||
|
||||
$this->entries[$dirName] = new ZipOutputEmptyDirEntry($entry);
|
||||
}
|
||||
@@ -440,14 +472,19 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
throw new IllegalArgumentException("stream is not resource");
|
||||
}
|
||||
$entryName = (string)$entryName;
|
||||
if (empty($entryName)) {
|
||||
if (strlen($entryName) === 0) {
|
||||
throw new IllegalArgumentException("Incorrect entry name " . $entryName);
|
||||
}
|
||||
$this->validateCompressionMethod($compressionMethod);
|
||||
|
||||
$fstat = fstat($stream);
|
||||
$mode = sprintf('%o', $fstat['mode']);
|
||||
$externalAttributes = (octdec($mode) & 0xffff) << 16;
|
||||
|
||||
$entry = new ZipEntry($entryName);
|
||||
$entry->setMethod($compressionMethod);
|
||||
$entry->setTime(time());
|
||||
$entry->setExternalAttributes($externalAttributes);
|
||||
|
||||
$this->entries[$entryName] = new ZipOutputStreamEntry($stream, $entry);
|
||||
}
|
||||
@@ -479,7 +516,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
if (!is_dir($inputDir)) {
|
||||
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");
|
||||
}
|
||||
if (empty($globPattern)) {
|
||||
@@ -507,8 +544,11 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
foreach ($filesFound as $file) {
|
||||
$filename = str_replace($inputDir, $moveToPath, $file);
|
||||
$filename = ltrim($filename, '/');
|
||||
is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
|
||||
is_file($file) && $this->addFromFile($file, $filename, $compressionMethod);
|
||||
if (is_dir($file)) {
|
||||
FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
|
||||
} elseif (is_file($file)) {
|
||||
$this->addFromFile($file, $filename, $compressionMethod);
|
||||
}
|
||||
}
|
||||
return $this->count() > $count;
|
||||
}
|
||||
@@ -564,8 +604,11 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
foreach ($files as $file) {
|
||||
$filename = str_replace($inputDir, $moveToPath, $file);
|
||||
$filename = ltrim($filename, '/');
|
||||
is_dir($file) && FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
|
||||
is_file($file) && $this->addFromFile($file, $filename, $compressionMethod);
|
||||
if (is_dir($file)) {
|
||||
FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
|
||||
} elseif (is_file($file)) {
|
||||
$this->addFromFile($file, $filename, $compressionMethod);
|
||||
}
|
||||
}
|
||||
return $this->count() > $count;
|
||||
}
|
||||
@@ -805,6 +848,18 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $align
|
||||
*/
|
||||
public function setZipAlign($align = 4)
|
||||
{
|
||||
if ($align === null) {
|
||||
$this->align = null;
|
||||
return;
|
||||
}
|
||||
$this->align = (int)$align;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save as file
|
||||
*
|
||||
@@ -875,7 +930,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
}
|
||||
|
||||
if (ZipEntry::UNKNOWN === $entry->getPlatform()) {
|
||||
$entry->setRawPlatform(ZipEntry::getCurrentPlatform());
|
||||
$entry->setRawPlatform(ZipEntry::PLATFORM_UNIX);
|
||||
}
|
||||
if (ZipEntry::UNKNOWN === $entry->getTime()) {
|
||||
$entry->setTime(time());
|
||||
@@ -999,6 +1054,10 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
|
||||
$offset = ftell($outputHandle);
|
||||
|
||||
// Commit changes.
|
||||
$entry->setGeneralPurposeBitFlags($general);
|
||||
$entry->setRawOffset($offset);
|
||||
|
||||
// Start changes.
|
||||
// local file header signature 4 bytes (0x04034b50)
|
||||
// version needed to extract 2 bytes
|
||||
@@ -1012,6 +1071,22 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
// file name length 2 bytes
|
||||
// extra field length 2 bytes
|
||||
$extra = $entry->getRawExtraFields();
|
||||
|
||||
// zip align
|
||||
$padding = 0;
|
||||
if ($this->align !== null && !$entry->isEncrypted() && $entry->getMethod() === ZipEntry::METHOD_STORED) {
|
||||
$padding =
|
||||
(
|
||||
$this->align -
|
||||
(
|
||||
$offset +
|
||||
self::LOCAL_FILE_HEADER_MIN_LEN +
|
||||
strlen($entry->getName()) +
|
||||
strlen($extra)
|
||||
) % $this->align
|
||||
) % $this->align;
|
||||
}
|
||||
|
||||
fwrite($outputHandle, pack('VvvvVVVVvv',
|
||||
ZipConstants::LOCAL_FILE_HEADER_SIG,
|
||||
$entry->getVersionNeededToExtract(),
|
||||
@@ -1022,15 +1097,16 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
$dd ? 0 : (int)$entry->getRawCompressedSize(),
|
||||
$dd ? 0 : (int)$entry->getRawSize(),
|
||||
strlen($entry->getName()),
|
||||
strlen($extra)
|
||||
strlen($extra) + $padding
|
||||
));
|
||||
// file name (variable size)
|
||||
fwrite($outputHandle, $entry->getName());
|
||||
// extra field (variable size)
|
||||
fwrite($outputHandle, $extra);
|
||||
// Commit changes.
|
||||
$entry->setGeneralPurposeBitFlags($general);
|
||||
$entry->setRawOffset($offset);
|
||||
|
||||
if ($padding > 0) {
|
||||
fwrite($outputHandle, str_repeat(chr(0), $padding));
|
||||
}
|
||||
|
||||
fwrite($outputHandle, $entryContent);
|
||||
|
||||
@@ -1059,7 +1135,6 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
. " (expected compressed entry size of "
|
||||
. $entry->getCompressedSize() . " bytes, but is actually " . $compressedSize . " bytes)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1229,7 +1304,7 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
public function outputAsAttachment($outputFilename, $mimeType = null)
|
||||
{
|
||||
$outputFilename = (string)$outputFilename;
|
||||
if (empty($outputFilename)) {
|
||||
if (strlen($outputFilename) === 0) {
|
||||
throw new IllegalArgumentException("Output filename is empty.");
|
||||
}
|
||||
if (empty($mimeType) || !is_string($mimeType)) {
|
||||
@@ -1321,13 +1396,13 @@ class ZipOutputFile implements \Countable, \ArrayAccess, \Iterator, ZipConstants
|
||||
*/
|
||||
public function offsetSet($entryName, $uncompressedDataContent)
|
||||
{
|
||||
if(empty($entryName)){
|
||||
$entryName = (string)$entryName;
|
||||
if (strlen($entryName) === 0) {
|
||||
throw new IllegalArgumentException('Entry name empty');
|
||||
}
|
||||
if($entryName[strlen($entryName)-1] === '/'){
|
||||
if ($entryName[strlen($entryName) - 1] === '/') {
|
||||
$this->addEmptyDir($entryName);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
$this->addFromString($entryName, $uncompressedDataContent);
|
||||
}
|
||||
}
|
||||
|
@@ -279,11 +279,9 @@ class ZipTest extends ZipTestCase
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
$outputZipFile = new ZipOutputFile($zipFile);
|
||||
$outputZipFile = ZipOutputFile::openFromFile($this->outputFilename);
|
||||
$outputZipFile->rename($oldName, $newName);
|
||||
$outputZipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
@@ -309,7 +307,7 @@ class ZipTest extends ZipTestCase
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
$outputZipFile = new ZipOutputFile($zipFile);
|
||||
$outputZipFile = $zipFile->edit();
|
||||
$outputZipFile->deleteFromName($deleteEntryName);
|
||||
$outputZipFile->saveAsFile($this->outputFilename);
|
||||
$zipFile->close();
|
||||
@@ -752,7 +750,7 @@ class ZipTest extends ZipTestCase
|
||||
*/
|
||||
public function testSetPassword()
|
||||
{
|
||||
$password = CryptoUtil::randomBytes(100);
|
||||
$password = base64_encode(CryptoUtil::randomBytes(100));
|
||||
$badPassword = "sdgt43r23wefe";
|
||||
|
||||
$outputZip = ZipOutputFile::create();
|
||||
@@ -761,6 +759,8 @@ class ZipTest extends ZipTestCase
|
||||
$outputZip->saveAsFile($this->outputFilename);
|
||||
$outputZip->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
|
||||
// set bad password Traditional Encryption
|
||||
@@ -791,6 +791,8 @@ class ZipTest extends ZipTestCase
|
||||
$outputZip->close();
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename, $password);
|
||||
|
||||
// check from WinZip AES encryption
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
|
||||
@@ -848,7 +850,7 @@ class ZipTest extends ZipTestCase
|
||||
'data' => CryptoUtil::randomBytes(255),
|
||||
'password' => CryptoUtil::randomBytes(255),
|
||||
'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' => [
|
||||
'data' => CryptoUtil::randomBytes(255),
|
||||
@@ -1022,8 +1024,50 @@ class ZipTest extends ZipTestCase
|
||||
$zipFile->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test zip alignment.
|
||||
*/
|
||||
public function testZipAlign()
|
||||
{
|
||||
$zipOutputFile = ZipOutputFile::create();
|
||||
|
||||
for ($i = 0; $i < 100; $i++) {
|
||||
$zipOutputFile->addFromString(
|
||||
'entry' . $i . '.txt',
|
||||
CryptoUtil::randomBytes(mt_rand(100, 4096)),
|
||||
ZipEntry::METHOD_STORED
|
||||
);
|
||||
}
|
||||
$zipOutputFile->saveAsFile($this->outputFilename);
|
||||
$zipOutputFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$result = self::doZipAlignVerify($this->outputFilename);
|
||||
if($result === null) return; // zip align not installed
|
||||
|
||||
// check not zip align
|
||||
self::assertFalse($result);
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
$zipOutputFile = ZipOutputFile::openFromZipFile($zipFile);
|
||||
$zipOutputFile->setZipAlign(4);
|
||||
$zipOutputFile->saveAsFile($this->outputFilename);
|
||||
$zipOutputFile->close();
|
||||
$zipFile->close();
|
||||
|
||||
self::assertCorrectZipArchive($this->outputFilename);
|
||||
|
||||
$result = self::doZipAlignVerify($this->outputFilename);
|
||||
self::assertNotNull($result);
|
||||
|
||||
// check zip align
|
||||
self::assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test support ZIP64 ext (slow test - normal).
|
||||
* Create > 65535 files in archive and open and extract to /dev/null.
|
||||
*/
|
||||
public function testCreateAndOpenZip64Ext()
|
||||
{
|
||||
@@ -1040,8 +1084,8 @@ class ZipTest extends ZipTestCase
|
||||
|
||||
$zipFile = ZipFile::openFromFile($this->outputFilename);
|
||||
self::assertEquals($zipFile->count(), $countFiles);
|
||||
foreach ($zipFile->getListFiles() as $entry) {
|
||||
$zipFile->getEntryContent($entry);
|
||||
foreach ($zipFile as $entry => $content) {
|
||||
strlen($content);
|
||||
}
|
||||
$zipFile->close();
|
||||
}
|
||||
|
@@ -9,18 +9,47 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* Assert correct zip archive.
|
||||
*
|
||||
* @param $filename
|
||||
* @param string $filename
|
||||
* @param string|null $password
|
||||
*/
|
||||
public static function assertCorrectZipArchive($filename)
|
||||
public static function assertCorrectZipArchive($filename, $password = null)
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && `which zip`) {
|
||||
exec("zip -T " . escapeshellarg($filename), $output, $returnCode);
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) {
|
||||
$command = "unzip";
|
||||
if ($password !== null) {
|
||||
$command .= " -P " . escapeshellarg($password);
|
||||
}
|
||||
$command .= " -t " . escapeshellarg($filename);
|
||||
exec($command, $output, $returnCode);
|
||||
|
||||
$output = implode(PHP_EOL, $output);
|
||||
|
||||
self::assertEquals($returnCode, 0);
|
||||
self::assertNotContains('zip error', $output);
|
||||
self::assertContains(' OK', $output);
|
||||
if ($password !== null && $returnCode === 81) {
|
||||
if(`which 7z`){
|
||||
// WinZip 99-character limit
|
||||
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
|
||||
$password = substr($password, 0, 99);
|
||||
|
||||
$command = "7z t -p" . escapeshellarg($password). " " . escapeshellarg($filename);
|
||||
exec($command, $output, $returnCode);
|
||||
|
||||
$output = implode(PHP_EOL, $output);
|
||||
|
||||
self::assertEquals($returnCode, 0);
|
||||
self::assertNotContains(' Errors', $output);
|
||||
self::assertContains(' Ok', $output);
|
||||
}
|
||||
else{
|
||||
fwrite(STDERR, 'Program unzip cannot support this function.'.PHP_EOL);
|
||||
fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full'.PHP_EOL);
|
||||
}
|
||||
}
|
||||
else {
|
||||
self::assertEquals($returnCode, 0);
|
||||
self::assertNotContains('incorrect password', $output);
|
||||
self::assertContains(' OK', $output);
|
||||
self::assertContains('No errors', $output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,4 +71,19 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
|
||||
self::assertEquals(file_get_contents($filename), $actualEmptyZipData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @return bool|null If null - can not install zipalign
|
||||
*/
|
||||
public static function doZipAlignVerify($filename)
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
|
||||
exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
|
||||
return $returnCode === 0;
|
||||
} else {
|
||||
fwrite(STDERR, 'Can not find program "zipalign" for test');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user