Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP ZIP 解壓縮,檔名編碼自動轉為Utf-8編碼範例
#!/usr/bin/env php
<?php
/**
* PHP ZIP 解壓縮,檔名編碼自動轉為Utf-8編碼範例。
*
* @see https://gist.github.com/samwhelp/2f6764561bc3b5f15125
* @see http://www.ubuntu-tw.org/modules/newbb/viewtopic.php?post_id=327124#forumpost327124
* @see http://micy.cn/blog/post/118
* @see http://www.chengcms.com/study/391.html?jdfwkey=wtgln3
* @see http://forum.ubuntu.org.cn/viewtopic.php?f=163&t=325386
* @see http://php.net/manual/en/function.mb-detect-encoding.php
* @see http://php.net/manual/en/function.iconv.php
* @see http://php.net/manual/en/book.iconv.php
* @see http://php.net/manual/en/book.zip.php
* @see http://php.net/manual/en/zip.examples.php
*/
/*
這個是在Xubuntu 14.04 底下測試的。
將檔案存到「~/bin/phpunzip」,然後「chmod u+x ~/bin/phpunzip」。
然後解壓縮,只要執行「phpunzip file_name 」就行了。
*/
if (!extension_loaded('zip')) {
printf("php zip extension is needed. See http://www.php.net/manual/en/zip.installation.php\n");
exit;
}
if (!isset($argv[1])) {
printf("Usage: %s file_name\n\n", basename($argv[0]));
exit;
}
define('UTIL_ZIP_EXTRACT_DETECT_ENCODING_ORDER_LIST', 'UTF-8, BIG5, ISO-8859-1'); //請更改你要檢測的編碼。請參考「http://php.net/manual/en/function.mb-detect-order.php」。
//define('UTIL_ZIP_EXTRACT_DETECT_ENCODING_ORDER_LIST', 'UTF-8, GB2312, BIG-5, SHIFT-JIS, ISO-8859-1');
/**
* ZIP 解壓縮,並偵測檔案名稱編碼,自動轉換成Utf-8。
*
* @param string $source_path zip檔案路徑。
* @param mixed $target_path 解開後的路徑,可以不填。
* @return string 有發生錯誤,會回傳訊息。
*/
function util_zip_extract($source_path, $target_path=null)
{
if ($target_path === null) {
$target_path = substr($source_path, 0, -4);
}
$target_base_path = dirname($target_path);
if (file_exists($target_path)) {
return 'Dir or File is exist!: ' . $target_path;
}
if (!file_exists($source_path)) {
return 'Extract file is not exist!: ' . $source_path;
}
$file = zip_open($source_path);
if (!is_resource($file)) {
return 'Open zip file not ok!: ' . $source_path;
}
while ($entry = zip_read($file)) {
$file_size = zip_entry_filesize($entry);
$file_name = zip_entry_name($entry);
$encode = mb_detect_encoding($file_name, UTIL_ZIP_EXTRACT_DETECT_ENCODING_ORDER_LIST);
//echo $encode . PHP_EOL;
if (strtoupper(trim($encode)) !== 'UTF-8') {
$file_name = iconv($encode, 'UTF-8//TRANSLIT', $file_name);
////$file_name = iconv('Big5', 'UTF-8//TRANSLIT', $file_name);
////$file_name = iconv('Big-5', 'UTF-8//TRANSLIT', $file_name);
}
$target_file_path = $target_base_path . '/' . $file_name;
if (!$file_size) {
if (file_exists($target_file_path)) {
continue;
}
mkdir($target_file_path, 0755, true);
continue;
} else if (!zip_entry_open($file, $entry)) {
continue;
}
file_put_contents($target_file_path, zip_entry_read($entry, $file_size));
zip_entry_close($entry);
}
zip_close($file);
}
////$file = getcwd() . '/' . $argv[1];
$file = $argv[1];
$msg = util_zip_extract($file);
if ($msg) {
echo $msg;
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.