質問編集履歴
2
コードを変更しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,20 +10,41 @@
|
|
10
10
|
|
11
11
|
コードを載せますので、ぜひご教授ください。
|
12
12
|
|
13
|
-
public function extractionZip($data = null)
|
13
|
+
public function extractionZip($data = null, $destDir = TMP, $toEncoding = 'UTF-8')
|
14
14
|
{
|
15
|
+
if (setlocale(LC_ALL, 'ja_JP.UTF-8') === false) {
|
16
|
+
error_log('Locale not found: ja_JP.UTF-8');
|
17
|
+
exit();
|
18
|
+
}
|
19
|
+
$zip = new ZipArchive;
|
15
20
|
$zipFile = TMP . 'zip/' . $data['Zip']['name'];
|
16
|
-
$
|
21
|
+
$hex = file_get_contents($zipFile, false, null, 7, 1);
|
22
|
+
$fromEncoding = (ord($hex) & 0x08) === 0x08 ? 'UTF-8' : 'CP932';
|
17
|
-
$
|
23
|
+
$sameEncoding = $fromEncoding === $toEncoding;
|
18
24
|
$res = $zip->open($zipFile);
|
19
|
-
if ($res ===
|
25
|
+
if ($res === false){
|
20
|
-
if (setlocale(LC_ALL, 'ja_JP.UTF-8') === false) {
|
21
|
-
|
26
|
+
return false;
|
22
|
-
|
27
|
+
exit();
|
23
|
-
}
|
24
|
-
$dir = $zip->extractTo(TMP . 'zip');
|
25
|
-
$zip->close();
|
26
|
-
}else{
|
27
|
-
echo 'error';
|
28
28
|
}
|
29
|
+
$num_files = $zip->numFiles;
|
30
|
+
|
31
|
+
for ($i = 0; $i < $num_files; $i++) {
|
32
|
+
$stat = $zip->statIndex($i);
|
33
|
+
$name = $sameEncoding ? $stat['name']: mb_convert_encoding($stat['name'], $toEncoding, $fromEncoding);
|
34
|
+
if ($name[strlen($name)-1] === '/') {
|
35
|
+
continue;
|
29
|
-
|
36
|
+
}
|
37
|
+
|
38
|
+
$splited = explode('/', $name);
|
39
|
+
$baseName = array_pop($splited);
|
40
|
+
$dir = empty($splited) ? $destDir. implode('/', $splited). '/' : $destDir ;
|
41
|
+
if (is_dir($dir) === false) {
|
42
|
+
mkdir($dir, 0777, true);
|
43
|
+
}
|
44
|
+
file_put_contents("{$dir}{$baseName}", $zip->getFromIndex($i), LOCK_EX);
|
45
|
+
}
|
46
|
+
$dir = $zip->extractTo(TMP . 'zip');
|
47
|
+
$zip->close();
|
48
|
+
}
|
49
|
+
|
50
|
+
ご回答を参考にして書いたコードです。解凍はするのですが、ディレクトリ名が日本語だと文字化けし、圧縮したファイル名が日本語だとそれも文字化けします。英語やlinuxで圧縮したファイルであれば問題ありません。
|
1
コードに少しミスがあったため
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,12 +15,13 @@
|
|
15
15
|
$zipFile = TMP . 'zip/' . $data['Zip']['name'];
|
16
16
|
$zipFile = mb_convert_encoding($zipFile, 'UTF-8');
|
17
17
|
$zip = new ZipArchive;
|
18
|
+
$res = $zip->open($zipFile);
|
18
19
|
if ($res === true){
|
19
20
|
if (setlocale(LC_ALL, 'ja_JP.UTF-8') === false) {
|
20
21
|
error_log('Locale not found: ja_JP.UTF-8');
|
21
22
|
exit();
|
22
23
|
}
|
23
|
-
$dir = $zip->extractTo(TMP . 'zip')
|
24
|
+
$dir = $zip->extractTo(TMP . 'zip');
|
24
25
|
$zip->close();
|
25
26
|
}else{
|
26
27
|
echo 'error';
|