note 110719 added to class.ziparchive

From: Date: Wed, 28 Nov 2012 03:48:17 +0000
Subject: note 110719 added to class.ziparchive
Groups: php.notes 
Request: Send a blank email to php-notes+get-192168@lists.php.net to get a copy of this message
Zip a folder (include itself). Usage: HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); <?php class HZip { /** * 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 static function folderToZip($folder, &$zipFile, $exclusiveLength) { $handle = opendir($folder); while ($f = readdir($handle)) { if ($f != '.' && $f != '..') { $filePath = "$folder/$f"; // Remove prefix from file path before add to zip. $localPath = substr($filePath, $exclusiveLength); if (is_file($filePath)) { $zipFile->addFile($filePath, $localPath); } elseif (is_dir($filePath)) { // Add sub-directory. $zipFile->addEmptyDir($localPath); self::folderToZip($filePath, $zipFile, $exclusiveLength); } } } closedir($handle); } /** * Zip a folder (include itself). * Usage: * HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); * * @param string $sourcePath Path of directory to be zip. * @param string $outZipPath Path of output zip file. */ public static function zipDir($sourcePath, $outZipPath) { $pathInfo = pathInfo($sourcePath); $parentPath = $pathInfo['dirname']; $dirName = $pathInfo['basename']; $z = new ZipArchive(); $z->open($outZipPath, ZIPARCHIVE::CREATE); $z->addEmptyDir($dirName); self::folderToZip($sourcePath, $z, strlen("$parentPath/")); $z->close(); } } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 126.15.153.52 ---- Manual Page -- http://www.php.net/manual/en/class.ziparchive.php Edit -- https://master.php.net/note/edit/110719 Del: integrated -- https://master.php.net/note/delete/110719/integrated Del: useless -- https://master.php.net/note/delete/110719/useless Del: bad code -- https://master.php.net/note/delete/110719/bad+code Del: spam -- https://master.php.net/note/delete/110719/spam Del: non-english -- https://master.php.net/note/delete/110719/non-english Del: in docs -- https://master.php.net/note/delete/110719/in+docs Del: other reasons-- https://master.php.net/note/delete/110719 Reject -- https://master.php.net/note/reject/110719 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#192168) next »