Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzxjohn/8f22fb63bb67fabe3436761564c1850b to your computer and use it in GitHub Desktop.
Save wzxjohn/8f22fb63bb67fabe3436761564c1850b to your computer and use it in GitHub Desktop.
Linux/MacOS下解压中文乱码的zip包
<?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