[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/thirdparty/ -> dZip.inc.php (source)

   1  <?php
   2  /**
   3   * DOWNLOADED FROM: http://www.phpclasses.org/browse/package/2495/
   4   * License:     BSD License
   5   */
   6  ?>
   7  <?php
   8  class dZip{
   9      var $filename;
  10      var $overwrite;
  11      
  12      var $zipSignature = "\x50\x4b\x03\x04"; // local file header signature
  13      var $dirSignature = "\x50\x4b\x01\x02"; // central dir header signature
  14      var $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir signature
  15      var $files_count  = 0;
  16      var $fh;
  17      
  18      Function dZip($filename, $overwrite=true){
  19          $this->filename  = $filename;
  20          $this->overwrite = $overwrite;
  21      }
  22      Function addDir($dirname, $fileComments=''){
  23          if(substr($dirname, -1) != '/')
  24              $dirname .= '/';
  25          $this->addFile(false, $dirname, $fileComments);
  26      }
  27      Function addFile($filename, $cfilename, $fileComments='', $data=false){
  28          if(!($fh = &$this->fh))
  29              $fh = fopen($this->filename, $this->overwrite?'wb':'a+b');
  30          
  31          // $filename can be a local file OR the data wich will be compressed
  32          if(substr($cfilename, -1)=='/'){
  33              $details['uncsize'] = 0;
  34              $data = '';
  35          }
  36          elseif(file_exists($filename)){
  37              $details['uncsize'] = filesize($filename);
  38              $data = file_get_contents($filename);
  39          }
  40          elseif($filename){
  41              echo "<b>Cannot add $filename. File not found</b><br>";
  42              return false;
  43          }
  44          else{
  45              $details['uncsize'] = strlen($data); // Prasad: Fixed instead of strlen($filename)
  46              // DATA is given.. use it! :|
  47          }
  48          
  49          // if data to compress is too small, just store it
  50          if($details['uncsize'] < 256){
  51              $details['comsize'] = $details['uncsize'];
  52              $details['vneeded'] = 10;
  53              $details['cmethod'] = 0;
  54              $zdata = &$data;
  55          }
  56          else{ // otherwise, compress it
  57              $zdata = gzcompress($data);
  58              $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug (thanks to Eric Mueller)
  59              $details['comsize'] = strlen($zdata);
  60              $details['vneeded'] = 10;
  61              $details['cmethod'] = 8;
  62          }
  63          
  64          $details['bitflag'] = 0;
  65          $details['crc_32']  = crc32($data);
  66          
  67          // Convert date and time to DOS Format, and set then
  68          $lastmod_timeS  = str_pad(decbin(date('s')>=32?date('s')-32:date('s')), 5, '0', STR_PAD_LEFT);
  69          $lastmod_timeM  = str_pad(decbin(date('i')), 6, '0', STR_PAD_LEFT);
  70          $lastmod_timeH  = str_pad(decbin(date('H')), 5, '0', STR_PAD_LEFT);
  71          $lastmod_dateD  = str_pad(decbin(date('d')), 5, '0', STR_PAD_LEFT);
  72          $lastmod_dateM  = str_pad(decbin(date('m')), 4, '0', STR_PAD_LEFT);
  73          $lastmod_dateY  = str_pad(decbin(date('Y')-1980), 7, '0', STR_PAD_LEFT);
  74          
  75          # echo "ModTime: $lastmod_timeS-$lastmod_timeM-$lastmod_timeH (".date("s H H").")\n";
  76          # echo "ModDate: $lastmod_dateD-$lastmod_dateM-$lastmod_dateY (".date("d m Y").")\n";
  77          $details['modtime'] = bindec("$lastmod_timeH$lastmod_timeM$lastmod_timeS");
  78          $details['moddate'] = bindec("$lastmod_dateY$lastmod_dateM$lastmod_dateD");
  79          
  80          $details['offset'] = ftell($fh);
  81          fwrite($fh, $this->zipSignature);
  82          fwrite($fh, pack('s', $details['vneeded'])); // version_needed
  83          fwrite($fh, pack('s', $details['bitflag'])); // general_bit_flag
  84          fwrite($fh, pack('s', $details['cmethod'])); // compression_method
  85          fwrite($fh, pack('s', $details['modtime'])); // lastmod_time
  86          fwrite($fh, pack('s', $details['moddate'])); // lastmod_date
  87          fwrite($fh, pack('V', $details['crc_32']));  // crc-32
  88          fwrite($fh, pack('I', $details['comsize'])); // compressed_size
  89          fwrite($fh, pack('I', $details['uncsize'])); // uncompressed_size
  90          fwrite($fh, pack('s', strlen($cfilename)));   // file_name_length
  91          fwrite($fh, pack('s', 0));  // extra_field_length
  92          fwrite($fh, $cfilename);    // file_name
  93          // ignoring extra_field
  94          fwrite($fh, $zdata);
  95          
  96          // Append it to central dir
  97          $details['external_attributes']  = (substr($cfilename, -1)=='/'&&!$zdata)?16:32; // Directory or file name
  98          $details['comments']             = $fileComments;
  99          $this->appendCentralDir($cfilename, $details);
 100          $this->files_count++;
 101      }
 102      Function setExtra($filename, $property, $value){
 103          $this->centraldirs[$filename][$property] = $value;
 104      }
 105      Function save($zipComments=''){
 106          if(!($fh = &$this->fh))
 107              $fh = fopen($this->filename, $this->overwrite?'w':'a+');
 108          
 109          $cdrec = "";
 110          foreach($this->centraldirs as $filename=>$cd){
 111              $cdrec .= $this->dirSignature;
 112              $cdrec .= "\x0\x0";                  // version made by
 113              $cdrec .= pack('v', $cd['vneeded']); // version needed to extract
 114              $cdrec .= "\x0\x0";                  // general bit flag
 115              $cdrec .= pack('v', $cd['cmethod']); // compression method
 116              $cdrec .= pack('v', $cd['modtime']); // lastmod time
 117              $cdrec .= pack('v', $cd['moddate']); // lastmod date
 118              $cdrec .= pack('V', $cd['crc_32']);  // crc32
 119              $cdrec .= pack('V', $cd['comsize']); // compressed filesize
 120              $cdrec .= pack('V', $cd['uncsize']); // uncompressed filesize
 121              $cdrec .= pack('v', strlen($filename)); // file comment length
 122              $cdrec .= pack('v', 0);                // extra field length
 123              $cdrec .= pack('v', strlen($cd['comments'])); // file comment length
 124              $cdrec .= pack('v', 0); // disk number start
 125              $cdrec .= pack('v', 0); // internal file attributes
 126              $cdrec .= pack('V', $cd['external_attributes']); // internal file attributes
 127              $cdrec .= pack('V', $cd['offset']); // relative offset of local header
 128              $cdrec .= $filename;
 129              $cdrec .= $cd['comments'];
 130          }
 131          $before_cd = ftell($fh);
 132          fwrite($fh, $cdrec);
 133          
 134          // end of central dir
 135          fwrite($fh, $this->dirSignatureE);
 136          fwrite($fh, pack('v', 0)); // number of this disk
 137          fwrite($fh, pack('v', 0)); // number of the disk with the start of the central directory
 138          fwrite($fh, pack('v', $this->files_count)); // total # of entries "on this disk" 
 139          fwrite($fh, pack('v', $this->files_count)); // total # of entries overall 
 140          fwrite($fh, pack('V', strlen($cdrec)));     // size of central dir 
 141          fwrite($fh, pack('V', $before_cd));         // offset to start of central dir
 142          fwrite($fh, pack('v', strlen($zipComments))); // .zip file comment length
 143          fwrite($fh, $zipComments);
 144          
 145          fclose($fh);
 146      }
 147      
 148      // Private
 149      Function appendCentralDir($filename,$properties){
 150          $this->centraldirs[$filename] = $properties;
 151      }
 152  }
 153  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1