Skip to content

Commit

Permalink
Impl(Task/Ext/Archive/ZipTask): savefileattributes flag
Browse files Browse the repository at this point in the history
By default false when omitted, file permissions attribute are not saved.
When setting the savefileattributes to true, files permissions
are savec as external attributes in the Zip archive.

Example:

```xml
<zip includeemptydirs="true" destfile="${project.basedir}/test.zip" basedir="./dir" savefileattributes="true">
    <fileset dir="dir">
        <include name="**/**"/>
    </fileset>
</zip>
```
  • Loading branch information
leagris committed May 2, 2024
1 parent a1979b2 commit b44b6e7
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions src/Phing/Task/Ext/Archive/ZipTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ZipTask extends MatchingTask

private $ignoreLinks = false;

private $saveFileAttributes = false;

/**
* File path prefix in zip archive
*
Expand All @@ -73,6 +75,38 @@ class ZipTask extends MatchingTask
*/
private $comment = '';

/**
* Propagates the directory's mTime and permissions in the Zip archive if supported.
*
* @param \ZipArchive $zip The Zip archive
* @param File $f The File handle to the directory
* @param string $pathInZip The path of the directory in the Zip archive
*
* @throws IOException
*/
private static function fixDirAttributes($zip, $f, $pathInZip)
{
$indexInZip = $zip->locateName('/' === mb_substr($pathInZip, -1) ? $pathInZip : $pathInZip . '/');
if (false !== $indexInZip) {
$zip->setMtimeIndex($indexInZip, $f->lastModified());
$filePerms = fileperms($f->getPath());
if (false !== $filePerms) { // filePerms supported
$zip->setExternalAttributesIndex($indexInZip, \ZipArchive::OPSYS_DEFAULT, $filePerms << 16);
}
}
}
/**
* Removes all external attributes from the Zip archive.
*
* @param \ZipArchive $zip The Zip archive
*/
private static function clearExternalAttributes($zip)
{
for ($i = 0, $count = $zip->count(); $i < $count; ++$i) {
$zip->setExternalAttributesIndex($i, \ZipArchive::OPSYS_DOS, null);
}
}

/**
* Add a new fileset.
*
Expand Down Expand Up @@ -150,6 +184,17 @@ public function setIgnoreLinks($bool)
$this->ignoreLinks = (bool) $bool;
}

/**
* Set the save file attributes flag.
*
* @param bool $bool Flag if file attributes should be saved
* @return void
*/
public function setSaveFileAttributes($bool)
{
$this->saveFileAttributes = (bool) $bool;
}

/**
* Add a comment to the zip archive.
*
Expand Down Expand Up @@ -236,6 +281,10 @@ public function main()

$this->addFilesetsToArchive($zip);

if (!$this->saveFileAttributes) {
self::clearExternalAttributes($zip);
}

$zip->close();
} catch (IOException $ioe) {
$msg = "Problem creating ZIP: " . $ioe->getMessage();
Expand Down Expand Up @@ -306,12 +355,7 @@ private function addFilesetsToArchive($zip)
if ($f->isDirectory()) {
if ($pathInZip != '.') {
$zip->addEmptyDir($pathInZip);
$filePerms = fileperms($f->getPath());
if (false !== $filePerms) { // filePerms supported
$dirAttrs = $filePerms << 16;
$dirAttrName = $pathInZip . '/';
$zip->setExternalAttributesName($dirAttrName, \ZipArchive::OPSYS_UNIX, $dirAttrs);
}
static::fixDirAttributes($zip, $f, $pathInZip);
}
} else {
$zip->addFile($f->getAbsolutePath(), $pathInZip);
Expand Down

0 comments on commit b44b6e7

Please sign in to comment.