| | 1 | <?php |
|---|
| | 2 | /** |
|---|
| | 3 | * @package jelix |
|---|
| | 4 | * @subpackage utils |
|---|
| | 5 | * @author Bastien Jaillot |
|---|
| | 6 | * @copyright 2008 Bastien Jaillot |
|---|
| | 7 | * @link http://www.jelix.org |
|---|
| | 8 | * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html |
|---|
| | 9 | */ |
|---|
| | 10 | |
|---|
| | 11 | /** |
|---|
| | 12 | * Class to create a zip file. |
|---|
| | 13 | * @package jelix |
|---|
| | 14 | * @subpackage utils |
|---|
| | 15 | * @link http://www.pkware.com/business_and_developers/developer/appnote/ Official ZIP file format |
|---|
| | 16 | * @link adapted from http://forums.b2evolution.net/viewtopic.php?t=12110 |
|---|
| | 17 | */ |
|---|
| | 18 | class jZipCreator2 { |
|---|
| | 19 | |
|---|
| | 20 | /** |
|---|
| | 21 | * file name which appear in the browser |
|---|
| | 22 | */ |
|---|
| | 23 | protected $zipFilename=''; |
|---|
| | 24 | |
|---|
| | 25 | |
|---|
| | 26 | protected $zip = null; |
|---|
| | 27 | |
|---|
| | 28 | |
|---|
| | 29 | protected $useZipCreator = true; |
|---|
| | 30 | |
|---|
| | 31 | protected $parent_dir_path = null; |
|---|
| | 32 | |
|---|
| | 33 | function jZipCreator2(){ |
|---|
| | 34 | $this->zipFilename = JELIX_APP_TEMP_PATH."file-".time().".zip"; |
|---|
| | 35 | |
|---|
| | 36 | if ( class_exists('ZipArchive', false) ) { |
|---|
| | 37 | $this->zip = new ZipArchive(); |
|---|
| | 38 | |
|---|
| | 39 | if ( $this->zip->open($this->zipFilename, ZIPARCHIVE::CREATE) !== TRUE ) |
|---|
| | 40 | throw new jException('jelix~errors.zip.cantbecreated', $this->zipFilename); |
|---|
| | 41 | } |
|---|
| | 42 | else |
|---|
| | 43 | $this->useZipCreator = false; |
|---|
| | 44 | |
|---|
| | 45 | |
|---|
| | 46 | |
|---|
| | 47 | } |
|---|
| | 48 | |
|---|
| | 49 | /** |
|---|
| | 50 | * adds a physical file to the zip archive |
|---|
| | 51 | * |
|---|
| | 52 | * @param string $filename the path of the physical file you want to add |
|---|
| | 53 | * @param string $zipPath the path of the file inside the zip archive |
|---|
| | 54 | */ |
|---|
| | 55 | public function addFile($filename, $zipFileName=''){ |
|---|
| | 56 | if($zipFileName == '') $zipFileName = $filename; |
|---|
| | 57 | if(file_exists($filename)){ |
|---|
| | 58 | if ($this->useZipCreator) |
|---|
| | 59 | $this->zip->addFile($filename,$zipFileName); |
|---|
| | 60 | else { |
|---|
| | 61 | if (!isset($this->parent_dir_path)) |
|---|
| | 62 | $this->parent_dir_path = dirname($filename)."/"; |
|---|
| | 63 | $this->zip .= " ". str_replace($this->parent_dir_path, '', $filename); |
|---|
| | 64 | |
|---|
| | 65 | } |
|---|
| | 66 | // $this->addContentFile($zipFileName, file_get_contents($filename), filemtime($filename)); |
|---|
| | 67 | }else{ |
|---|
| | 68 | throw new jException('jelix~errors.file.notexists', $filename); |
|---|
| | 69 | } |
|---|
| | 70 | } |
|---|
| | 71 | |
|---|
| | 72 | /** |
|---|
| | 73 | * adds the content of a directory to the zip archive |
|---|
| | 74 | * |
|---|
| | 75 | * @param string $path the path of the physical directory you want to add |
|---|
| | 76 | */ |
|---|
| | 77 | public function addDir($path, $zipDirPath='', $recursive = false){ |
|---|
| | 78 | if(file_exists($path)){ |
|---|
| | 79 | if($zipDirPath !='' && substr($zipDirPath,-1,1) != '/') |
|---|
| | 80 | $zipDirPath.='/'; |
|---|
| | 81 | if(substr($path,-1,1) != '/') |
|---|
| | 82 | $path.='/'; |
|---|
| | 83 | |
|---|
| | 84 | if (!isset($this->parent_dir_path)) |
|---|
| | 85 | $this->parent_dir_path = dirname($path)."/"; |
|---|
| | 86 | |
|---|
| | 87 | if ($handle = opendir($path)) { |
|---|
| | 88 | while (($file = readdir($handle)) !== false) { |
|---|
| | 89 | if($file == "." || $file == "..") continue; |
|---|
| | 90 | if (!is_dir($path.$file)) { |
|---|
| | 91 | $this->addFile($path.$file, $zipDirPath.$file); |
|---|
| | 92 | }elseif ($recursive){ |
|---|
| | 93 | $this->addDir($path.$file,$zipDirPath.$file, true); |
|---|
| | 94 | } |
|---|
| | 95 | } |
|---|
| | 96 | closedir($handle); |
|---|
| | 97 | } |
|---|
| | 98 | }else{ |
|---|
| | 99 | throw new jException('jelix~errors.file.notexists', $path); |
|---|
| | 100 | } |
|---|
| | 101 | } |
|---|
| | 102 | |
|---|
| | 103 | /** |
|---|
| | 104 | * create the contenu of the zip file |
|---|
| | 105 | * @return string the content of the zip file |
|---|
| | 106 | */ |
|---|
| | 107 | public function getContent(){ |
|---|
| | 108 | |
|---|
| | 109 | if ($this->useZipCreator) { |
|---|
| | 110 | $this->zip->close(); |
|---|
| | 111 | |
|---|
| | 112 | } |
|---|
| | 113 | else { |
|---|
| | 114 | chdir($this->parent_dir_path); |
|---|
| | 115 | `zip -rq $this->zipFilename $this->zip`; |
|---|
| | 116 | chdir(JELIX_APP_PATH); |
|---|
| | 117 | } |
|---|
| | 118 | |
|---|
| | 119 | return $this->zipFilename; |
|---|
| | 120 | |
|---|
| | 121 | |
|---|
| | 122 | } |
|---|
| | 123 | } |
|---|
| | 124 | |
|---|
| | 125 | ?> |