note 128542 added to class.ziparchive

From: Date: Tue, 16 May 2023 07:43:33 +0000
Subject: note 128542 added to class.ziparchive
Groups: php.notes 
Request: Send a blank email to php-notes+get-220498@lists.php.net to get a copy of this message
About ten years ago today umbalaconmeogia created a recursive version. With newer PHP features, time to revisit: <?php class MakeZip { private ZipArchive $zipArchive; private int $startPathLength; // chars to remove from the start for the stored entity public function __construct( string $zipArchivename, public readonly string $startPath, public readonly mixed $convert_function, ) { $this->zipArchive = new \ZipArchive; $this->zipArchive->open($zipArchivename, ZipArchive::CREATE); $this->startPathLength = strlen($this-> startPath); $this-> zipDir($startPath); } public function __destruct() { $this-> zipArchive-> close(); } /** * Add files and sub-directories in a folder to zip file. * @param string $folder * @param ZipArchive $zipFile * @param int $exclusiveLength Number of text to be exclusived from the file path. */ private function zipDir($folder) { echo $folder . '<br>' . PHP_EOL; foreach (new \DirectoryIterator($folder) as $f) { if ($f->isDot()) continue; //skip . .. if ($f->isDir()) { $this-> zipArchive-> addEmptyDir($f->getPathname()); $this-> zipDir($f->getPathname()); continue; } if ($f->isFile()) { if ($f->getBasename() === basename(__FILE__)) continue; // skip self if ($f->getExtension() === 'zip') continue; // skip ZIP files $this-> zipArchive ->addFile( substr($f-> getPathname(), $this-> startPathLength) ); // remove './' continue; } } } } ?> I've added a convert function for converting the file/foldernames from a windows environment. Use this function on windows: <?php $convert_function = fn ($localPath) => iconv("CP858//IGNORE", "UTF-8", $localPath); ?> and this on any other system : <?php $convert_function = fn ($localPath) => $localPath; ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 46.140.241.51) ---- Manual Page -- https://php.net/manual/en/class.ziparchive.php Edit -- https://main.php.net/note/edit/128542 Del: integrated -- https://main.php.net/note/delete/128542/integrated Del: useless -- https://main.php.net/note/delete/128542/useless Del: bad code -- https://main.php.net/note/delete/128542/bad+code Del: spam -- https://main.php.net/note/delete/128542/spam Del: non-english -- https://main.php.net/note/delete/128542/non-english Del: in docs -- https://main.php.net/note/delete/128542/in+docs Del: other reasons-- https://main.php.net/note/delete/128542 Reject -- https://main.php.net/note/reject/128542 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220498) next »