note 95227 deleted from ref.zip by danbrown
| From: | danbrown@php.net | Date: | Sat, 19 Dec 2009 21:23:41 +0000 |
| Subject: | note 95227 deleted from ref.zip by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164141@lists.php.net to get a copy of this message | ||
Note Submitter: a.s.suhov at gmail dot com
----
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;
?>