note 95227 added to ref.zip
| From: | a dot s dot suhovatgmaildotcom at osu1 dot php dot net | Date: | Sat, 19 Dec 2009 21:22:11 +0000 |
| Subject: | note 95227 added to ref.zip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164140@lists.php.net to get a copy of this message | ||
Here is modification of nheimann's class, which works correctly and creates archives with
correct Cyrillyc encoding (CP866) for Windows (even 7-zip don't understand UTF-8 and even
CP1251 in file names).
<?php
class FlxZipArchive extends ZipArchive {
//@authors Nicolas Heimann, Alexander Sukhov (changes and CP866)
public function addDir($location,$name) {
$slash = "/";
//$slash = iconv('UTF-8', 'CP866', "/");
if (!empty($name)) {
$name = iconv('UTF-8', 'CP866', $name);
$this->addEmptyDir($name);
}
if ($dir = opendir($location)) {
while ($file = readdir($dir)) {
if ($file != "." && $file != "..") {
$file_cp866 = iconv('UTF-8', 'CP866', $file);
if (is_dir($location."/".$file)) {
if (empty($name))
$this->addDir($location."/".$file,$file_cp866);
else
$this->addDir($location."/".$file,$name.$slash.$file_cp866);
}
else {
if (empty($name))
$this->addFile($location."/".$file,$file_cp866);
else
$this->addFile($location."/".$file,$name.$slash.$file_cp866);
}
}
}
closedir($location);
}
}
}
?>
Use:
<?php
//archive folder "files/nit" recursively and send it to browser
$folder = "files";
$opened_path = "files/nit";
$archive = new FlxZipArchive;
$archive->open($folder."/".$folder.".zip",ZIPARCHIVE::CREATE);
$archive->addDir($opened_path,"");
$archive->close();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;
filename=\"".str_replace("/",".",$opened_path).".zip"."\"");
header("Content-length: " .filesize($folder."/".$folder.".zip")
. "\n\n");
$zip = fopen($folder."/".$folder.".zip","rb");
fpassthru($zip);
fclose($zip);
unlink($folder."/".$folder.".zip");
die;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 77.232.15.123
----
Manual Page -- http://www.php.net/manual/en/ref.zip.php
Edit -- https://master.php.net/note/edit/95227
Del: integrated -- https://master.php.net/note/delete/95227/integrated
Del: useless -- https://master.php.net/note/delete/95227/useless
Del: bad code -- https://master.php.net/note/delete/95227/bad+code
Del: spam -- https://master.php.net/note/delete/95227/spam
Del: non-english -- https://master.php.net/note/delete/95227/non-english
Del: in docs -- https://master.php.net/note/delete/95227/in+docs
Del: other reasons-- https://master.php.net/note/delete/95227
Reject -- https://master.php.net/note/reject/95227
Search -- https://master.php.net/manage/user-notes.php