Skip to content

Commit

Permalink
Setting external attributes for new zip entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne-Lexa committed Oct 14, 2016
1 parent 294e3d5 commit 2c40215
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 26 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/vendor
*.iml
/.idea/
/.idea
/composer.lock
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

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.

Expand Down Expand Up @@ -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
//(
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nelexa/zip",
"description": "Zip files CRUD. Open, create, update, extract and get info tool. Support read and write encrypted archives. Support ZIP64 ext and zip align. 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",
Expand Down
122 changes: 122 additions & 0 deletions src/PhpZip/Model/ZipInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -150,6 +175,11 @@ class ZipInfo
*/
private $version;

/**
* @var string
*/
private $attributes;

/**
* ZipInfo constructor.
*
Expand Down Expand Up @@ -183,6 +213,88 @@ public function __construct(ZipEntry $entry)
$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);
}

/**
Expand Down Expand Up @@ -245,6 +357,7 @@ public function toArray()
'modified' => $this->getMtime(),
'created' => $this->getCtime(),
'accessed' => $this->getAtime(),
'attributes' => $this->getAttributes(),
'encrypted' => $this->isEncrypted(),
'comment' => $this->getComment(),
'crc' => $this->getCrc(),
Expand Down Expand Up @@ -358,6 +471,14 @@ public function getVersion()
return $this->version;
}

/**
* @return string
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @return string
*/
Expand All @@ -375,6 +496,7 @@ function __toString()
. (!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()
. '}';
Expand Down
48 changes: 28 additions & 20 deletions src/PhpZip/ZipOutputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ public static function openFromZipFile(ZipFile $zipFile)
return new self($zipFile);
}

/**
* Count zip entries.
*
* @return int
*/
public function count()
{
return sizeof($this->entries);
}

/**
* Returns the list files.
*
Expand Down Expand Up @@ -300,9 +310,12 @@ public function addFromString($entryName, $data, $compressionMethod = ZipEntry::
}
$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);
}
Expand Down Expand Up @@ -367,26 +380,15 @@ public function addDir(
foreach ($files as $file) {
$filename = str_replace($inputDir, $moveToPath, $file);
$filename = ltrim($filename, '/');
if(is_dir($file)){
if (is_dir($file)) {
FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
}
elseif(is_file($file)){
} 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.
*
Expand All @@ -401,12 +403,15 @@ public function addEmptyDir($dirName)
}
$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);
}
Expand Down Expand Up @@ -456,9 +461,14 @@ public function addFromStream($stream, $entryName, $compressionMethod = ZipEntry
}
$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);
}
Expand Down Expand Up @@ -518,10 +528,9 @@ public function addFilesFromGlob(
foreach ($filesFound as $file) {
$filename = str_replace($inputDir, $moveToPath, $file);
$filename = ltrim($filename, '/');
if(is_dir($file)){
if (is_dir($file)) {
FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
}
elseif(is_file($file)){
} elseif (is_file($file)) {
$this->addFromFile($file, $filename, $compressionMethod);
}
}
Expand Down Expand Up @@ -579,10 +588,9 @@ public function addFilesFromRegex(
foreach ($files as $file) {
$filename = str_replace($inputDir, $moveToPath, $file);
$filename = ltrim($filename, '/');
if(is_dir($file)){
if (is_dir($file)) {
FilesUtil::isEmptyDir($file) && $this->addEmptyDir($filename);
}
elseif(is_file($file)){
} elseif (is_file($file)) {
$this->addFromFile($file, $filename, $compressionMethod);
}
}
Expand Down Expand Up @@ -906,7 +914,7 @@ private function writeEntry($outputHandle, ZipOutputEntry $outputEntry)
}

if (ZipEntry::UNKNOWN === $entry->getPlatform()) {
$entry->setRawPlatform(ZipEntry::getCurrentPlatform());
$entry->setRawPlatform(ZipEntry::PLATFORM_UNIX);
}
if (ZipEntry::UNKNOWN === $entry->getTime()) {
$entry->setTime(time());
Expand Down

0 comments on commit 2c40215

Please sign in to comment.