{"id":118,"date":"2010-12-26T23:34:45","date_gmt":"2010-12-26T15:34:45","guid":{"rendered":"http:\/\/micy.cn\/blog\/?p=118"},"modified":"2010-12-26T23:34:45","modified_gmt":"2010-12-26T15:34:45","slug":"linuxmacos%e4%b8%8b%e8%a7%a3%e5%8e%8b%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81%e7%9a%84zip%e5%8c%85","status":"publish","type":"post","link":"http:\/\/micy.cn\/blog\/2010\/12\/26\/linuxmacos%e4%b8%8b%e8%a7%a3%e5%8e%8b%e4%b8%ad%e6%96%87%e4%b9%b1%e7%a0%81%e7%9a%84zip%e5%8c%85\/","title":{"rendered":"linux\/macOS\u4e0b\u89e3\u538b\u4e2d\u6587\u4e71\u7801\u7684zip\u5305"},"content":{"rendered":"<p>\u4ece windows \u4e0b\u538b\u7f29\u7684zip\u5305\u5982\u679c\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u540d\u6709\u4e2d\u6587\uff0c\u5728\u5178\u578b\u7684 linux \u73af\u5883\uff08\u5982ubuntu\uff09\u548c macOS \u4e0b\u6309\u7167\u6b63\u5e38zip\u89e3\u538b\u90fd\u4f1a\u4e71\u7801\u3002google\u4e00\u4e0b\u641c\u5230\u7684\u7b54\u6848\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662fhack\u4e00\u4e0bunzip\u7684\u4ee3\u7801\u91cd\u65b0\u7f16\u8bd1\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u53e6\u5916\u5199\u4e00\u4e2a\u89e3\u538b\u7684\u5de5\u5177\u4ee3\u66ffunzip\u3002<\/p>\n<p>\u5176\u5b9e\u8fd9\u662f\u4e00\u4e2a\u56f0\u6270\u4e86linuxer\u5f88\u591a\u5e74\u7684\u8001\u95ee\u9898\u4e86\uff0c\u80fd\u641c\u5230\u597d\u591a\u4eba\u95ee\u548c\u7b54\uff0c\u4f46\u81f3\u5c11\u5728\u534a\u5e74\u524d(6\u6708\u4efd)\u6211\u9047\u5230\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u8ba9\u6211\u6ee1\u610f\u7684\u7b80\u5355\u7684\u89e3\u51b3\u901a\u7528\u7684\u65b9\u6848\u3002<\/p>\n<p>\u5f53\u65f6\u6211\u662f\uff0c\u9047\u5230\u4ea7\u54c1\u7684\u540c\u4e8b\u7ed9\u4e86\u4e00\u4e2a\u505a\u597d\u7684\u4ea4\u4e92\u539f\u578b\uff0c\u8f93\u51fa\u4e00\u4e2azip\u5305\uff0c\u800c\u57fa\u672c\u4e0a\u516c\u53f8\u91cc\u5176\u5b83\u6280\u672f\u90fd\u662f\u7528linux\u6216\u8005macOS\u3002\u53c2\u8003\u4e86<a href=\"http:\/\/hi.baidu.com\/ichuan\/blog\/item\/96a6a6f290f7e85b342acc47.html\" target=\"_blank\" rel=\"noopener\">\u641c\u5230\u7684\u89e3\u51b3\u65b9\u6848<\/a>\uff0c\u7528 php \u7684 zip \u6269\u5c55\u6765\u89e3\u538b\uff0c\u5e76\u8f6c\u6362 GBK \u4e3a UTF-8 \u5373\u53ef\u3002<\/p>\n<p>\u5b58\u5230\u81ea\u5df1home\u76ee\u5f55\u4e0b\u7684bin\u91cc\u9762\uff0c \u6bd4\u5982\u653e\u5230 ~\/bin\/punzip\uff0c\u7136\u540e chmod +x ~\/bin\/punzip\uff0c\u4ee5\u540e\u5c31\u53ef\u4ee5\u6bd4\u8f83\u81ea\u7531\u7684\u76f4\u63a5\u8c03\u7528 punzip foo.zip \u6765\u89e3\u538b\u4e86\u3002<\/p>\n<p>\u4f9d\u8d56\u4e86 php \u7684 zip \u6269\u5c55\uff0c\u5982\u679c\u6267\u884c\u65f6\u63d0\u793a\u201cphp zip extension is needed.\u201d\uff08\u6bd4\u5982\u9ed8\u8ba4macOS\u5c31\u6ca1\u6709\u52a0\u8f7dzip\u6269\u5c55\uff0cubuntu\u6709\uff09\uff0c\u9700\u8981\u4fee\u6539 php.ini \u6dfb\u52a0 extension=zip.so\uff0c\u5177\u4f53\u6309\u7167\u63d0\u793a\u4e2d\u7684\u624b\u518c\u9875\u9762\u505a\u5c31\u597d\u3002<\/p>\n<p><code><span style=\"color: #000000\"><br \/>\n<span style=\"color: #0000BB\">&lt;?php<br \/><\/span><span style=\"color: #FF8000\">\/**<br \/>&nbsp;*&nbsp;\u89e3\u538b\u542b\u6709\u6587\u4ef6\u540d\u4e3agbk\u7f16\u7801\u7684zip\u5305\uff0c\u5230\u5f53\u524d\u76ee\u5f55<br \/>&nbsp;*\/<br \/><\/span><span style=\"color: #007700\">if&nbsp;(!<\/span><span style=\"color: #0000BB\">extension_loaded<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'zip'<\/span><span style=\"color: #007700\">))&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">printf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"php&nbsp;zip&nbsp;extension&nbsp;is&nbsp;needed.&nbsp;See&nbsp;http:\/\/www.php.net\/manual\/en\/zip.installation.php\\n\"<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$argv<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">]);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;die;<br \/>}<br \/>if&nbsp;(!isset(<\/span><span style=\"color: #0000BB\">$argv<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">1<\/span><span style=\"color: #007700\">]))&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">printf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"Usage:&nbsp;php&nbsp;%s&nbsp;filename\\n\\n\"<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$argv<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">]);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;die;<br \/>}<br \/><\/span><span style=\"color: #0000BB\">$f&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">zip_open<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$argv<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">1<\/span><span style=\"color: #007700\">]);<br \/>while&nbsp;(<\/span><span style=\"color: #0000BB\">$e&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">zip_read<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">))&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$filesize&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">zip_entry_filesize<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$filename&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">iconv<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'GBK'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'UTF-8'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">zip_entry_name<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">$filesize<\/span><span style=\"color: #007700\">)&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">mkdir<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #007700\">);&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">zip_entry_open<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">))&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">file_put_contents<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">zip_entry_read<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$filesize<\/span><span style=\"color: #007700\">));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">\"$filesize\\t$filename\\n\"<\/span><span style=\"color: #007700\">;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">zip_entry_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">);<br \/>}&nbsp;&nbsp;&nbsp;<br \/><\/span><span style=\"color: #0000BB\">zip_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$f<\/span><span style=\"color: #007700\">);<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece windows \u4e0b\u538b\u7f29\u7684zip\u5305\u5982\u679c\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u540d\u6709\u4e2d\u6587\uff0c\u5728\u5178\u578b\u7684 linux \u73af\u5883\uff08\u5982ubuntu\uff09\u548c macOS \u4e0b\u6309\u7167\u6b63\u5e38zip\u89e3\u538b\u90fd\u4f1a\u4e71\u7801\u3002google\u4e00\u4e0b\u641c\u5230\u7684\u7b54\u6848\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662fhack\u4e00\u4e0bunzip\u7684\u4ee3\u7801\u91cd\u65b0\u7f16\u8bd1\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u53e6\u5916\u5199\u4e00\u4e2a\u89e3\u538b\u7684\u5de5\u5177\u4ee3\u66ffunzip\u3002 \u5176\u5b9e\u8fd9\u662f\u4e00\u4e2a\u56f0\u6270\u4e86linuxer\u5f88\u591a\u5e74\u7684\u8001\u95ee\u9898\u4e86\uff0c\u80fd\u641c\u5230\u597d\u591a\u4eba\u95ee\u548c\u7b54\uff0c\u4f46\u81f3\u5c11\u5728\u534a\u5e74\u524d(6\u6708\u4efd)\u6211\u9047\u5230\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u8ba9\u6211\u6ee1\u610f\u7684\u7b80\u5355\u7684\u89e3\u51b3\u901a\u7528\u7684\u65b9\u6848\u3002 \u5f53\u65f6\u6211\u662f\uff0c\u9047\u5230\u4ea7\u54c1\u7684\u540c\u4e8b\u7ed9\u4e86\u4e00\u4e2a\u505a\u597d\u7684\u4ea4\u4e92\u539f\u578b\uff0c\u8f93\u51fa\u4e00\u4e2azip\u5305\uff0c\u800c\u57fa\u672c\u4e0a\u516c\u53f8\u91cc\u5176\u5b83\u6280\u672f\u90fd\u662f\u7528linux\u6216\u8005macOS\u3002\u53c2\u8003\u4e86\u641c\u5230\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u7528 php \u7684 zip \u6269\u5c55\u6765\u89e3\u538b\uff0c\u5e76\u8f6c\u6362 GBK \u4e3a UTF-8 \u5373\u53ef\u3002 \u5b58\u5230\u81ea\u5df1home\u76ee\u5f55\u4e0b\u7684bin\u91cc\u9762\uff0c \u6bd4\u5982\u653e\u5230 ~\/bin\/punzip\uff0c\u7136\u540e chmod +x ~\/bin\/punzip\uff0c\u4ee5\u540e\u5c31\u53ef\u4ee5\u6bd4\u8f83\u81ea\u7531\u7684\u76f4\u63a5\u8c03\u7528 punzip foo.zip \u6765\u89e3\u538b\u4e86\u3002 \u4f9d\u8d56\u4e86 php \u7684 zip \u6269\u5c55\uff0c\u5982\u679c\u6267\u884c\u65f6\u63d0\u793a\u201cphp zip extension is needed.\u201d\uff08\u6bd4\u5982\u9ed8\u8ba4macOS\u5c31\u6ca1\u6709\u52a0\u8f7dzip\u6269\u5c55\uff0cubuntu\u6709\uff09\uff0c\u9700\u8981\u4fee\u6539 php.ini \u6dfb\u52a0 extension=zip.so\uff0c\u5177\u4f53\u6309\u7167\u63d0\u793a\u4e2d\u7684\u624b\u518c\u9875\u9762\u505a\u5c31\u597d\u3002 &lt;?php\/**&nbsp;*&nbsp;\u89e3\u538b\u542b\u6709\u6587\u4ef6\u540d\u4e3agbk\u7f16\u7801\u7684zip\u5305\uff0c\u5230\u5f53\u524d\u76ee\u5f55&nbsp;*\/if&nbsp;(!extension_loaded(&#8216;zip&#8217;))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;php&nbsp;zip&nbsp;extension&nbsp;is&nbsp;needed.&nbsp;See&nbsp;http:\/\/www.php.net\/manual\/en\/zip.installation.php\\n&#8221;,&nbsp;$argv[0]);&nbsp;&nbsp;&nbsp;&nbsp;die;}if&nbsp;(!isset($argv[1]))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;printf(&#8220;Usage:&nbsp;php&nbsp;%s&nbsp;filename\\n\\n&#8221;,&nbsp;$argv[0]);&nbsp;&nbsp;&nbsp;&nbsp;die;}$f&nbsp;=&nbsp;zip_open($argv[1]);while&nbsp;($e&nbsp;=&nbsp;zip_read($f))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;$filesize&nbsp;=&nbsp;zip_entry_filesize($e);&nbsp;&nbsp;&nbsp;&nbsp;$filename&nbsp;=&nbsp;iconv(&#8216;GBK&#8217;,&nbsp;&#8216;UTF-8&#8217;,&nbsp;zip_entry_name($e));&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!$filesize)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mkdir($filename);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(!zip_entry_open($f,&nbsp;$e))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents($filename,&nbsp;zip_entry_read($e,&nbsp;$filesize));&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&#8220;$filesize\\t$filename\\n&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zip_entry_close($e);}&nbsp;&nbsp;&nbsp;zip_close($f);<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":0,"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/micy.cn\/blog\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}