Ticket #538: patch-zip.diff

File patch-zip.diff, 5.9 kB (added by bastnic, 4 months ago)
  • lib/jelix/core/response/jResponseZip.class.php

    old new  
    33* @package     jelix 
    44* @subpackage  core_response 
    55* @author      Laurent Jouanneau 
    6 * @contributor 
    7 * @copyright   2006-2008 Laurent Jouanneau 
     6* @contributor Bastien Jaillot 
     7* @copyright   2006-2008 Laurent Jouanneau, 2008 Bastien Jaillot 
    88* @link        http://www.jelix.org 
    99* @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
    1010*/ 
     
    1212/** 
    1313 * 
    1414 */ 
    15 include JELIX_LIB_UTILS_PATH.'jZipCreator.class.php'; 
     15include JELIX_LIB_UTILS_PATH.'jZipCreator2.class.php'; 
    1616 
    1717/** 
    1818* generate a zip content and send it to the browser 
     
    4040    * constructor 
    4141    */ 
    4242    function __construct (){ 
    43         $this->content = new jZipCreator(); 
     43        $this->content = new jZipCreator2(); 
    4444        parent::__construct(); 
    4545    } 
    4646 
     
    5959        $this->addHttpHeader('Content-Description','File Transfert',false); 
    6060        $this->addHttpHeader('Content-Transfer-Encoding','binary',false); 
    6161        $this->addHttpHeader('Pragma','no-cache',false); 
    62         $this->addHttpHeader('Cache-Control','no-store, no-cache, must-revalidate, post-check=0, pre-check=0',false); 
    63         $this->addHttpHeader('Expires','0',false); 
     62        $this->addHttpHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0',false); 
     63        $this->addHttpHeader('Expires', gmdate('D, d M Y H:i:s') . ' GMT',false); 
    6464 
    65         $this->_httpHeaders['Content-length']=strlen($zipContent); 
     65        $this->_httpHeaders['Content-length']=filesize($zipContent); 
    6666        $this->sendHttpHeaders(); 
    67         echo $zipContent
     67        readfile($zipContent)
    6868        flush(); 
     69                unlink($zipContent); 
    6970        return true; 
    7071    } 
    7172 
  • lib/jelix/utils/jZipCreator2.class.php

    old new  
     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 */ 
     18class 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?> 
Download in other formats: Original Format