XMLWriter.php
10 years ago
chunk.php
10 years ago
config.php
10 years ago
download.php
10 years ago
handler.php
10 years ago
helper.php
10 years ago
input.php
10 years ago
session.php
10 years ago
zip.php
10 years ago
zip.php
54 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists('PMXE_Zip')){ |
| 4 | |
| 5 | class PMXE_Zip |
| 6 | { |
| 7 | /** |
| 8 | * Add files and sub-directories in a folder to zip file. |
| 9 | * @param string $folder |
| 10 | * @param ZipArchive $zipFile |
| 11 | * @param int $exclusiveLength Number of text to be exclusived from the file path. |
| 12 | */ |
| 13 | private static function folderToZip($folder, &$zipFile, $exclusiveLength) { |
| 14 | $handle = opendir($folder); |
| 15 | while (false !== $f = readdir($handle)) { |
| 16 | if ($f != '.' && $f != '..') { |
| 17 | $filePath = "$folder/$f"; |
| 18 | // Remove prefix from file path before add to zip. |
| 19 | $localPath = substr($filePath, $exclusiveLength); |
| 20 | if (is_file($filePath)) { |
| 21 | $zipFile->addFile($filePath, $localPath); |
| 22 | } elseif (is_dir($filePath)) { |
| 23 | // Add sub-directory. |
| 24 | $zipFile->addEmptyDir($localPath); |
| 25 | self::folderToZip($filePath, $zipFile, $exclusiveLength); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | closedir($handle); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Zip a folder (include itself). |
| 34 | * Usage: |
| 35 | * PMXE_Zip::zipDir('/path/to/sourceDir', '/path/to/out.zip'); |
| 36 | * |
| 37 | * @param string $sourcePath Path of directory to be zip. |
| 38 | * @param string $outZipPath Path of output zip file. |
| 39 | */ |
| 40 | public static function zipDir($sourcePath, $outZipPath) |
| 41 | { |
| 42 | $pathInfo = pathInfo($sourcePath); |
| 43 | $parentPath = $pathInfo['dirname']; |
| 44 | $dirName = $pathInfo['basename']; |
| 45 | |
| 46 | $z = new ZipArchive(); |
| 47 | $z->open($outZipPath, ZIPARCHIVE::CREATE); |
| 48 | $z->addEmptyDir($dirName); |
| 49 | self::folderToZip($sourcePath, $z, strlen("$parentPath/")); |
| 50 | $z->close(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | } |