Ticket #570: 570-jZipCreator-empty-dirs-support.diff

File 570-jZipCreator-empty-dirs-support.diff, 4.3 kB (added by Julien, 4 months ago)

patch with empty dirs support

  • lib/jelix/utils/jZipCreator.class.php

    old new  
    6464                $path.='/'; 
    6565 
    6666            if ($handle = opendir($path)) { 
     67                $this->addEmptyDir($zipDirPath,filemtime($path)); 
    6768                while (($file = readdir($handle)) !== false) { 
    6869                    if($file == "." || $file == "..") continue; 
    6970                    if (!is_dir($path.$file)) { 
     
    164165 
    165166        $this->centralDirectory[] = $cdrecord; 
    166167 
    167         $this ->centralDirOffset += strlen($filerecord); 
     168        $this->centralDirOffset += strlen($filerecord); 
    168169 
    169170    } 
     171     
     172    /** 
     173     * adds an empty dir to the zip file 
     174     */ 
     175    public function addEmptyDir($name, $time=0){ 
     176        // converts unix timestamp to dos binary format 
     177        if($time == 0) 
     178            $time = time(); 
     179        elseif($time < 315529200) // 01/01/1980 
     180            $time = 315529200; 
    170181 
     182        $dt = getdate($time); 
    171183 
     184        $time =  pack('V',($dt['seconds'] >> 1) | ($dt['minutes'] << 5) | ($dt['hours'] << 11) | 
     185                ($dt['mday'] << 16) | ($dt['mon'] << 21) | (($dt['year'] - 1980) << 25)); 
     186 
     187        /* 
     188        generation of the file record 
     189 
     190        file record: 
     191         - local file header signature     4 bytes  (0x04034b50) 
     192         - version needed to extract       2 bytes  14 
     193         - general purpose bit flag        2 bytes  0 
     194         - compression method              2 bytes  0x8 
     195         - last mod file time              2 bytes (fileinfo) 
     196         - last mod file date              2 bytes (fileinfo) 
     197         - crc-32                          4 bytes (fileinfo) 
     198         - compressed size                 4 bytes (fileinfo) 
     199         - uncompressed size               4 bytes (fileinfo) 
     200         - file name length                2 bytes (fileinfo) 
     201         - extra field length              2 bytes (here 0) (fileinfo) 
     202         - file name (variable size) 
     203         - extra field (variable size)      (here nothing) 
     204         - compressed content 
     205        */ 
     206        $name     = str_replace('\\', '/', $name); 
     207 
     208        $fileinfo  = $time.pack('V', crc32('')); 
     209        $fileinfo .= pack('V', 0). pack('V', 0); 
     210        $fileinfo .= pack('v', strlen($name))."\x00\x00"; 
     211 
     212        $filerecord   = "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00"; 
     213        $filerecord .= $fileinfo.$name; 
     214 
     215        $this->fileRecords[] = $filerecord; 
     216 
     217 
     218        /* 
     219         register the file into the central directory record 
     220         it contains an header for each file 
     221           - central file header signature   4 bytes  (0x02014b50) 
     222           - version made by                 2 bytes  0=DOS 
     223           - version needed to extract       2 bytes  0x14 
     224           - general purpose bit flag        2 bytes  0 
     225           - compression method              2 bytes  0x8 
     226           - last mod file time              2 bytes (fileinfo) 
     227           - last mod file date              2 bytes (fileinfo) 
     228           - crc-32                          4 bytes (fileinfo) 
     229           - compressed size                 4 bytes (fileinfo) 
     230           - uncompressed size               4 bytes (fileinfo) 
     231           - file name length                2 bytes (fileinfo) 
     232           - extra field length              2 bytes   0 (fileinfo) 
     233           - file comment length             2 bytes   0 
     234           - disk number start               2 bytes   0 
     235           - internal file attributes        2 bytes   0 
     236           - external file attributes        4 bytes   32 : 'archive' bit set 
     237           - relative offset of local header 4 bytes 
     238           - file name (variable size) 
     239           - extra field (variable size) 
     240           - file comment (variable size) 
     241        */ 
     242        $cdrecord = "\x50\x4b\x01\x02\x00\x00\x14\x00\x00\x00\x08\x00".$fileinfo; 
     243        $cdrecord .= "\x00\x00\x00\x00\x00\x00"; 
     244        $cdrecord .= pack('V', 16); 
     245        $cdrecord .= pack('V', $this ->centralDirOffset ); 
     246        $cdrecord .= $name; 
     247 
     248        $this->centralDirectory[] = $cdrecord; 
     249 
     250        $this->centralDirOffset += strlen($filerecord); 
     251    } 
     252 
     253 
    172254    /** 
    173255     * create the contenu of the zip file 
    174256     * @return  string  the content of the zip file 
Download in other formats: Original Format