php 生成 zip 压缩文件
Laiyong Wang Lv6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function addFileToZip($path, $zip, $dir)
{
$handler = opendir($path); //打开当前文件夹由$path指定。
while (($filename = readdir($handler)) !== false) {
if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’,不要对他们进行操作
if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归
$this->addFileToZip($path . "/" . $filename, $zip, $dir);
} else { //将文件加入zip对象
$file = file_get_contents($path . "/" . $filename);
$filename = str_replace($dir . '/', "", $path . "/" . $filename);//将多余的绝对路径替换,使生成的zip包仅有需要压缩的文件,就算方法的第三个参数,替换成空
$zip->addFromString($filename, $file);
}
}
}
@closedir($path);
}

$zip = new ZipArchive();
$zip->open(Yii::$app->params['saveDir'] . DIRECTORY_SEPARATOR . $paperInfo->guid . '.zip', ZipArchive::CREATE);
addFileToZip($saveDir, $zip, $saveDir); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close();

$li-margin-bottom = 0.8rem $post-tool-button-width = 2.5rem .post-tools-container { padding-top var(--component-gap) .post-tools-list { li { margin-bottom $li-margin-bottom &:last-child { margin-bottom 0 } } li.tools-item { position relative box-sizing border-box width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.2rem background var(--background-color-1) border-radius 50% box-shadow 2px 2px 5px var(--shadow-color) cursor pointer &:hover { box-shadow 2px 2px 8px var(--shadow-hover-color) } i { color var(--text-color-3) } &:hover { color var(--background-color-1) background var(--primary-color) i { color var(--background-color-1) !important } } &.toggle-show-toc { display none } &.go-to-comments { .post-comments-count { position absolute top 0 right -1rem display none align-items center justify-content center box-sizing border-box min-width 1.1rem height 1.1rem padding 0 0.2rem color var(--badge-color) font-size 12px background var(--badge-background-color) border-radius 0.4rem +keep-tablet() { display none !important } } } } li.status-item { width $post-tool-button-width height $post-tool-button-width color var(--text-color-3) font-size 1.6rem cursor pointer &.post-lock { cursor default .fa-lock-open { display none color var(--keep-success-color) } &.decrypt { cursor pointer .fa-lock-open { display block } .fa-lock { display none } } } } } }