php 压缩文件夹

2021-01-15 13:14

阅读:667

标签:参数   $path   hive   ==   lse   tput   fun   source   php   

php 压缩文件夹

例子来源于php官方文档。

addFile($filePath, $localPath);
                } elseif (is_dir($filePath)) {
                    // 如果是文件夹递归添加。
                    $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();
    }
}

php 压缩文件夹

标签:参数   $path   hive   ==   lse   tput   fun   source   php   

原文地址:https://www.cnblogs.com/bigcola/p/13391473.html


评论


亲,登录后才可以留言!