streams/Code/Lib/ExtendedZip.php

67 lines
1.7 KiB
PHP
Raw Normal View History

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
2022-02-16 04:08:28 +00:00
namespace Code\Lib;
2021-12-02 22:33:36 +00:00
use ZipArchive;
/**
* Description of ExtendedZip
*
* @author andrew
*/
2021-12-03 03:01:39 +00:00
class ExtendedZip extends ZipArchive
{
2022-10-23 21:18:44 +00:00
// Member function to add a whole file system subtree to the archive
2021-12-03 03:01:39 +00:00
public function addTree($dirname, $localname = '')
{
if ($localname) {
2022-10-23 21:18:44 +00:00
$this->addEmptyDir($localname);
2021-12-03 03:01:39 +00:00
}
2022-10-23 21:18:44 +00:00
$this->_addTree($dirname, $localname);
2021-12-03 03:01:39 +00:00
}
2022-10-23 21:18:44 +00:00
// Internal function, to recurse
2021-12-03 03:01:39 +00:00
protected function _addTree($dirname, $localname)
{
2022-10-23 21:18:44 +00:00
$dir = opendir($dirname);
if (!$dir) {
return;
}
2021-12-03 03:01:39 +00:00
while ($filename = readdir($dir)) {
2022-10-23 21:18:44 +00:00
// Discard . and ..
2021-12-03 03:01:39 +00:00
if ($filename == '.' || $filename == '..') {
continue;
}
2022-10-23 21:18:44 +00:00
// Proceed according to type
$path = $dirname . '/' . $filename;
$localpath = $localname ? ($localname . '/' . $filename) : $filename;
2021-12-03 03:01:39 +00:00
if (is_dir($path)) {
// Directory: add & recurse
$this->addEmptyDir($localpath);
$this->_addTree($path, $localpath);
2022-10-23 21:18:44 +00:00
}
elseif (is_file($path)) {
// File: just add
$this->addFile($path, $localpath);
2021-12-03 03:01:39 +00:00
}
}
2022-10-23 21:18:44 +00:00
closedir($dir);
2021-12-03 03:01:39 +00:00
}
// Helper function
public static function zipTree($dirname, $zipFilename, $flags = 0, $localname = '')
{
$zip = new self();
$zip->open($zipFilename, $flags);
$zip->addTree($dirname, $localname);
$zip->close();
}
}