Created
June 11, 2012 08:54
-
-
Save si1en2i0/2909160 to your computer and use it in GitHub Desktop.
Linux/MacOS下解压中文乱码的zip包
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 解压含有文件名为UTF-8编码的zip包,到当前目录 | |
* | |
* Mac OS X 系统自带的压缩程序对 zip 文件名用 UTF-8 编码 | |
* 但 zip 文件头中没有声明 PKZIP 高版本增加的 Unicode 位。 | |
* Windows 会认为文件名是 ANSI 编码,结果显示乱码。 | |
* | |
* @uses zipcorrect.php file.zip | |
* | |
* @see http://micy.cn/blog/post/118 | |
*/ | |
if (!extension_loaded('zip')) { | |
printf("php zip extension is needed. See http://www.php.net/manual/en/zip.installation.php\n", $argv[0]); | |
die; | |
} | |
if (!isset($argv[1])) { | |
printf("Usage: php %s filename\n\n", $argv[0]); | |
die; | |
} | |
$f = zip_open($argv[1]); | |
while ($e = zip_read($f)) { | |
$filesize = zip_entry_filesize($e); | |
$filename = iconv('UTF-8', 'GBK', zip_entry_name($e)); | |
if (!$filesize) { | |
mkdir($filename); | |
continue; | |
} else if (!zip_entry_open($f, $e)) { | |
continue; | |
} | |
file_put_contents($filename, zip_entry_read($e, $filesize)); | |
echo "$filesize\t$filename\n"; | |
zip_entry_close($e); | |
} | |
zip_close($f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment