-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathZipDownloader.php
More file actions
235 lines (210 loc) · 5.83 KB
/
ZipDownloader.php
File metadata and controls
235 lines (210 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* NetCommonsZip
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('File', 'Utility');
App::uses('TemporaryFolder', 'Files.Utility');
App::uses('TemporaryFile', 'Files.Utility');
App::uses('NetCommonsFile', 'Files.Utility');
/**
* Class NetCommonsZip
* 基本的な利用シーンはZipしてダウンロードするテンポラリな使い方
*/
class ZipDownloader {
/**
* @var TemporaryFolder 作業用フォルダ
*/
protected $_tmpFolder;
/**
* @var string|null password
*/
protected $_password = null;
/**
* @var string zip filepath
*/
public $path;
/**
* @var bool ファイルオープン常態
*/
protected $_open = false;
/**
* @var string Zipコマンド
*/
protected $_zipCommand = 'zip';
/**
* @var string クライアントOSの文字コード
*/
protected $_clientOsEncoding;
/**
* ZipDownloader constructor.
*/
public function __construct() {
$this->_tmpFolder = new TemporaryFolder();
$this->_open = true;
$userAgent = Hash::get($_SERVER, 'HTTP_USER_AGENT');
if (stristr($userAgent, 'Mac')) {
// Macの場合
$this->_clientOsEncoding = 'UTF-8';
} elseif (stristr($userAgent, 'Windows')) {
// Windowsの場合
$this->_clientOsEncoding = 'SJIS-win';
} else {
$this->_clientOsEncoding = Configure::read('App.encoding');
}
}
/**
* close
*
* zipの作成
*
* @return bool
*/
public function close() {
// zip作成先
$zipSaveFolder = new TemporaryFolder();
$this->path = $zipSaveFolder->path . DS . Security::hash(mt_rand() . microtime(), 'md5') . '.zip';
if (strlen($this->_password)) {
// パスワードを使う
$cmd = $this->_zipCommand;
$execCmd = sprintf(
'%s -r -e -P %s %s %s',
$cmd,
escapeshellarg($this->_password),
escapeshellarg($this->path),
'*'
);
chdir($this->_tmpFolder->path);
// コマンドを実行する
//exec(($execCmd));
exec($execCmd, $output, $returnVar);
//CakeLog::debug($execCmd);
if ($returnVar > 0) {
CakeLog::debug(' Error:output=' . json_encode($output) . ', return_var=' . $returnVar);
return false;
}
} else {
// パスワード無しZIP
// ZipArchiverを使う
$zip = new ZipArchive();
$zip->open($this->path, ZipArchive::CREATE);
chdir($this->_tmpFolder->path);
list($folders, $files) = $this->_tmpFolder->tree();
foreach ($folders as $folder) {
if ($folder !== $this->_tmpFolder->path) {
$relativePath = str_replace($this->_tmpFolder->path . DS, '', $folder);
$zip->addEmptyDir($this->_convertFilename($relativePath));
}
}
foreach ($files as $file) {
$relativePath = str_replace($this->_tmpFolder->path . DS, '', $file);
$zip->addFile($relativePath, $this->_convertFilename($relativePath));
}
$zip->close();
}
$this->_open = false;
}
/**
* ファイル追加
*
* @param string $filePath 追加するファイルのパス
* @param string|null $localName ZIPに追加するときのファイル名
*
* @return void
* @throws InternalErrorException
*/
public function addFile($filePath, $localName = null) {
// ファイルをlocalNameにしてコピー
if ($localName === null) {
$localName = NetCommonsFile::basename($filePath);
}
$destPath = $this->_tmpFolder->path . DS . $localName;
$result = copy($filePath, $this->_convertFilename($destPath));
if (!$result) {
// 失敗
throw new InternalErrorException('NetCommonsZip File IO Error');
}
}
/**
* add from string
*
* @param string $localname zipファイルに追加するときのファイル名
* @param string $contents 追加するファイルの中身
*
* @return void
*/
public function addFromString($localname, $contents) {
// ε( v ゚ω゚) < 例外処理
$file = new File($this->_tmpFolder->path . DS . $localname, true);
$file->write($contents);
$file->close();
}
/**
* add folder
*
* @param string $folderPath zipに追加するフォルダのパス
*
* @return void
* @throws InternalErrorException
*/
public function addFolder($folderPath) {
$folder = new Folder($folderPath);
if (!$folder->copy($this->_tmpFolder->path . DS . NetCommonsFile::basename($folder->path))) {
throw new InternalErrorException('NetCommonsZip File IO Error');
}
}
/**
* set password
*
* @param string $password ZIPの解凍・圧縮に使うパスワード
*
* @return void
*/
public function setPassword($password) {
$this->_password = $password;
}
/**
* Download
*
* @param string $filename download時のファイル名
* @return CakeResponse ダウンロードレスポンス
*/
public function download($filename) {
// closeされてなかったらcloseする
if ($this->_open) {
$this->close();
}
// 全角文字が含まれたファイル名が指定されている場合
// encodeしてあげる
if (strlen($filename) != mb_strlen($filename)) {
$filename = rawurlencode($filename);
}
$response = new CakeResponse();
$response->type('application/zip');
$content = 'attachment;';
$content .= 'filename*=UTF-8\'\'' . $filename;
$response->header('Content-Disposition', $content);
$response->file($this->path);
return $response;
}
/**
* OSのファイルシステムにあわせて文字コード変換を行う
*
* @param string $name ファイル名
* @return string
*/
protected function _convertFilename($name) {
// Mac上でNC3をつかってるケースの対策
// Macファイルシステムでは濁点文字が2つの文字になるNFDなのをNFCに変換する
if (class_exists('Normalizer')) {
if (Normalizer::isNormalized($name, Normalizer::FORM_D)) {
$name = Normalizer::normalize($name, Normalizer::FORM_C);
}
}
$name = mb_convert_encoding($name, $this->_clientOsEncoding, 'ASCII,JIS,UTF-8,EUC-JP,SJIS');
return $name;
}
}