{"id":30692,"date":"2021-09-18T22:54:07","date_gmt":"2021-09-18T14:54:07","guid":{"rendered":"https:\/\/web4theme.com\/?p=30692"},"modified":"2021-09-18T22:54:26","modified_gmt":"2021-09-18T14:54:26","slug":"php-ziparchive%e5%a3%93%e7%b8%ae%e8%b3%87%e6%96%99%e5%a4%be%e6%95%99%e5%ad%b8%ef%bc%88%e6%ba%90%e7%a2%bc%e8%ac%9b%e8%a7%a3%ef%bc%89","status":"publish","type":"post","link":"https:\/\/web4theme.com\/?p=30692","title":{"rendered":"PHP ZipArchive\u58d3\u7e2e\u8cc7\u6599\u593e\u6559\u5b78\uff08\u6e90\u78bc\u8b1b\u89e3\uff09"},"content":{"rendered":"\n<p>\u7528php\u58d3\u7e2e\u8cc7\u6599\u593e\u662f\u65e5\u5e38\u5de5\u4f5c\u7684\u4e00\u90e8\u5206\uff0c\u4e0b\u9762\u4ee5\u771f\u5be6\u6e90\u78bc\u8b1b\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php line-numbers\">&lt;?php\n\n\/\/ \u793a\u4f8b\u6e90\u78bc\u58d3\u7e2e\u6307\u5b9a\u6587\u4ef6\u593e\uff0c\u58d3\u7e2e\u5f8c\u7684\u6587\u4ef6\u8207\u6e90\u6587\u4ef6\u593e\u5728\u76f8\u540c\u7684\u4f4d\u7f6e\n$fn = \"filename1.zip\"; \/\/ \u58d3\u7e2e\u5f8c\u7684\u6587\u4ef6\u540d\n$fd = \"\/Volumes\/folde1\"; \/\/ \u8981\u58d3\u7e2e\u7684\u6587\u4ef6\u593e\uff0c\u9019\u500b\u662flinux\u8def\u5f91\uff0c\u5728windows\u985e\u4f3c c:\\folder1\n\nzipFolder($fd.DIRECTORY_SEPARATOR.$fn, $fd.DIRECTORY_SEPARATOR.$fn);\n\nfunction zipFolder($folder, $zipFileName)\n{\n    \/\/ Get real path for our folder php\u6587\u4ef6\u904b\u884c\u65bcapache\u6216nginx\u7b49\u670d\u52d9\u5668\u74b0\u5883\uff0c\u9700\u8981\u7372\u53d6\u7269\u7406\u8def\u5f91\n    $rootPath = realpath($folder);\n\n    \/\/ Initialize archive object \u521d\u59cb\u5316ZipArchive\u5c0d\u8c61\n    $zip = new ZipArchive();\n    $zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE); \/\/ \u5275\u5efa\u58d3\u7e2e\u6587\u4ef6\n\n    \/\/ Create recursive directory iterator \u7372\u5f97\u905e\u6b78\u8a2a\u554f\u5668\uff0c\u652f\u6301\u591a\u7d1a\u5b50\u76ee\u9304\n    \/** @var SplFileInfo[] $files *\/\n    $files = new RecursiveIteratorIterator(\n        new RecursiveDirectoryIterator($rootPath),\n        RecursiveIteratorIterator::LEAVES_ONLY\n    );\n\n    foreach ($files as $name => $file) { \n        \/\/ Skip directories (they would be added automatically) \u8df3\u904e\u76ee\u9304\uff0c\u6703\u81ea\u52d5\u8655\u7406\uff08\u58d3\u7e2e\u6587\u4ef6\u7684\u6642\u5019\u5e36\u8def\u5f91\u5c31\u6703\u6709\u76ee\u9304\uff09\n        if (!$file->isDir()) {\n            \/\/ Get real and relative path for current file \u7372\u5f97\u6587\u4ef6\u7269\u7406\u8def\u5f91\n            $filePath = $file->getRealPath();\n            $relativePath = substr($filePath, strlen($rootPath) + 1);\n\n            \/\/ Add current file to archive \u52a0\u5165\u58d3\u7e2e\n            $zip->addFile($filePath, $relativePath);\n        }\n    }\n\n    \/\/ Zip archive will be created only after closing object \u95dc\u9589\u5f8c\u5beb\u5165\n    $zip->close();\n}<\/code><\/pre>\n\n\n\n<p>php\u904b\u884c\u74b0\u5883\u8981\u5df2\u7d93\u5b89\u88ddzip\u64f4\u5c55\u54e6<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662fPHP ZipArchive\u58d3\u7e2e\u8cc7\u6599\u593e\u6559\u5b78\uff0c\u5982\u6709\u4e0d\u6e05\u695a\u7684\u8acb\u7559\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528php\u58d3\u7e2e\u8cc7\u6599\u593e\u662f\u65e5\u5e38\u5de5\u4f5c\u7684\u4e00\u90e8\u5206\uff0c\u4e0b\u9762\u4ee5\u771f\u5be6\u6e90\u78bc\u8b1b\u89e3 php\u904b\u884c\u74b0\u5883\u8981\u5df2\u7d93\u5b89\u88ddzip\u64f4\u5c55\u54e6 \u4ee5\u4e0a\u5c31\u662fPHP ZipArchive\u58d3\u7e2e\u8cc7\u6599\u593e\u6559\u5b78\uff0c\u5982\u6709\u4e0d\u6e05\u695a\u7684\u8acb\u7559\u8a00\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103],"tags":[111],"class_list":["post-30692","post","type-post","status-publish","format-standard","hentry","category-webdesign-example","tag-php"],"_links":{"self":[{"href":"https:\/\/web4theme.com\/index.php?rest_route=\/wp\/v2\/posts\/30692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/web4theme.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web4theme.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web4theme.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/web4theme.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30692"}],"version-history":[{"count":2,"href":"https:\/\/web4theme.com\/index.php?rest_route=\/wp\/v2\/posts\/30692\/revisions"}],"predecessor-version":[{"id":30694,"href":"https:\/\/web4theme.com\/index.php?rest_route=\/wp\/v2\/posts\/30692\/revisions\/30694"}],"wp:attachment":[{"href":"https:\/\/web4theme.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web4theme.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web4theme.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}