Advertisement
Guest User

Untitled

a guest
Nov 1st, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.10 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Class to dynamically create a zip file (archive) of file(s) and/or directory
  4.  *
  5.  * W3Lessons.com Programming Blog
  6.  */
  7.  
  8. class CreateZipFile {
  9.  
  10.     public $compressedData = array();
  11.     public $centralDirectory = array(); // central directory
  12.     public $endOfCentralDirectory = "\x50\x4b\x05\x06\x00\x00\x00\x00"; //end of Central directory record
  13.     public $oldOffset = 0;
  14.  
  15.     /**
  16.      * Function to create the directory where the file(s) will be unzipped
  17.      *
  18.      * @param string $directoryName
  19.      * @access public
  20.      * @return void
  21.      */
  22.     public function addDirectory($directoryName) {
  23.         $directoryName = str_replace("\\", "/", $directoryName);
  24.         $feedArrayRow = "\x50\x4b\x03\x04";
  25.         $feedArrayRow .= "\x0a\x00";
  26.         $feedArrayRow .= "\x00\x00";
  27.         $feedArrayRow .= "\x00\x00";
  28.         $feedArrayRow .= "\x00\x00\x00\x00";
  29.         $feedArrayRow .= pack("V",0);
  30.         $feedArrayRow .= pack("V",0);
  31.         $feedArrayRow .= pack("V",0);
  32.         $feedArrayRow .= pack("v", strlen($directoryName) );
  33.         $feedArrayRow .= pack("v", 0 );
  34.         $feedArrayRow .= $directoryName;
  35.         $feedArrayRow .= pack("V",0);
  36.         $feedArrayRow .= pack("V",0);
  37.         $feedArrayRow .= pack("V",0);
  38.         $this->compressedData[] = $feedArrayRow;
  39.         $newOffset = strlen(implode("", $this->compressedData));
  40.         $addCentralRecord = "\x50\x4b\x01\x02";
  41.         $addCentralRecord .="\x00\x00";
  42.         $addCentralRecord .="\x0a\x00";
  43.         $addCentralRecord .="\x00\x00";
  44.         $addCentralRecord .="\x00\x00";
  45.         $addCentralRecord .="\x00\x00\x00\x00";
  46.         $addCentralRecord .= pack("V",0);
  47.         $addCentralRecord .= pack("V",0);
  48.         $addCentralRecord .= pack("V",0);
  49.         $addCentralRecord .= pack("v", strlen($directoryName) );
  50.         $addCentralRecord .= pack("v", 0 );
  51.         $addCentralRecord .= pack("v", 0 );
  52.         $addCentralRecord .= pack("v", 0 );
  53.         $addCentralRecord .= pack("v", 0 );
  54.         $addCentralRecord .= pack("V", 16 );
  55.         $addCentralRecord .= pack("V", $this->oldOffset );
  56.         $this->oldOffset = $newOffset;
  57.         $addCentralRecord .= $directoryName;
  58.         $this->centralDirectory[] = $addCentralRecord;
  59.     }
  60.  
  61.     /**
  62.      * Function to add file(s) to the specified directory in the archive
  63.      *
  64.      * @param string $directoryName
  65.      * @param string $data
  66.      * @return void
  67.      * @access public
  68.      */
  69.     public function addFile($data, $directoryName)   {
  70.         $directoryName = str_replace("\\", "/", $directoryName);
  71.         $feedArrayRow = "\x50\x4b\x03\x04";
  72.         $feedArrayRow .= "\x14\x00";
  73.         $feedArrayRow .= "\x00\x00";
  74.         $feedArrayRow .= "\x08\x00";
  75.         $feedArrayRow .= "\x00\x00\x00\x00";
  76.         $uncompressedLength = strlen($data);
  77.         $compression = crc32($data);
  78.         $gzCompressedData = gzcompress($data);
  79.         $gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2);
  80.         $compressedLength = strlen($gzCompressedData);
  81.         $feedArrayRow .= pack("V",$compression);
  82.         $feedArrayRow .= pack("V",$compressedLength);
  83.         $feedArrayRow .= pack("V",$uncompressedLength);
  84.         $feedArrayRow .= pack("v", strlen($directoryName) );
  85.         $feedArrayRow .= pack("v", 0 );
  86.         $feedArrayRow .= $directoryName;
  87.         $feedArrayRow .= $gzCompressedData;
  88.         $feedArrayRow .= pack("V",$compression);
  89.         $feedArrayRow .= pack("V",$compressedLength);
  90.         $feedArrayRow .= pack("V",$uncompressedLength);
  91.         $this->compressedData[] = $feedArrayRow;
  92.         $newOffset = strlen(implode("", $this->compressedData));
  93.         $addCentralRecord = "\x50\x4b\x01\x02";
  94.         $addCentralRecord .="\x00\x00";
  95.         $addCentralRecord .="\x14\x00";
  96.         $addCentralRecord .="\x00\x00";
  97.         $addCentralRecord .="\x08\x00";
  98.         $addCentralRecord .="\x00\x00\x00\x00";
  99.         $addCentralRecord .= pack("V",$compression);
  100.         $addCentralRecord .= pack("V",$compressedLength);
  101.         $addCentralRecord .= pack("V",$uncompressedLength);
  102.         $addCentralRecord .= pack("v", strlen($directoryName) );
  103.         $addCentralRecord .= pack("v", 0 );
  104.         $addCentralRecord .= pack("v", 0 );
  105.         $addCentralRecord .= pack("v", 0 );
  106.         $addCentralRecord .= pack("v", 0 );
  107.         $addCentralRecord .= pack("V", 32 );
  108.         $addCentralRecord .= pack("V", $this->oldOffset );
  109.         $this->oldOffset = $newOffset;
  110.         $addCentralRecord .= $directoryName;
  111.         $this->centralDirectory[] = $addCentralRecord;
  112.     }
  113.  
  114.     /**
  115.      * Function to return the zip file
  116.      *
  117.      * @return zipfile (archive)
  118.      * @access public
  119.      * @return void
  120.      */
  121.     public function getZippedfile() {
  122.         $data = implode("", $this->compressedData);
  123.         $controlDirectory = implode("", $this->centralDirectory);
  124.         return
  125.         $data.
  126.         $controlDirectory.
  127.         $this->endOfCentralDirectory.
  128.         pack("v", sizeof($this->centralDirectory)).
  129.         pack("v", sizeof($this->centralDirectory)).
  130.         pack("V", strlen($controlDirectory)).
  131.         pack("V", strlen($data)).
  132.         "\x00\x00";
  133.     }
  134.  
  135.     /**
  136.      *
  137.      * Function to force the download of the archive as soon as it is created
  138.      *
  139.      * @param archiveName string - name of the created archive file
  140.      * @access public
  141.      * @return ZipFile via Header
  142.      */
  143.     public function forceDownload($archiveName) {
  144.         if(ini_get('zlib.output_compression')) {
  145.             ini_set('zlib.output_compression', 'Off');
  146.         }
  147.  
  148.         // Security checks
  149.         if( $archiveName == "" ) {
  150.             echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> The download file was NOT SPECIFIED.</body></html>";
  151.             exit;
  152.         }
  153.         elseif ( ! file_exists( $archiveName ) ) {
  154.             echo "<html><title>Public Photo Directory - Download </title><body><BR><B>ERROR:</B> File not found.</body></html>";
  155.             exit;
  156.         }
  157.  
  158.         header("Pragma: public");
  159.         header("Expires: 0");
  160.         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  161.         header("Cache-Control: private",false);
  162.         header("Content-Type: application/zip");
  163.         header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
  164.         header("Content-Transfer-Encoding: binary");
  165.         header("Content-Length: ".filesize($archiveName));
  166.         readfile("$archiveName");
  167.     }
  168.  
  169.     /**
  170.       * Function to parse a directory to return all its files and sub directories as array
  171.       *
  172.       * @param string $dir
  173.       * @access protected
  174.       * @return array
  175.       */
  176.     protected function parseDirectory($rootPath, $seperator="/"){
  177.         $fileArray=array();
  178.         $handle = opendir($rootPath);
  179.         while( ($file = @readdir($handle))!==false) {
  180.             if($file !='.' && $file !='..'){
  181.                 if (is_dir($rootPath.$seperator.$file)){
  182.                     $array=$this->parseDirectory($rootPath.$seperator.$file);
  183.                     $fileArray=array_merge($array,$fileArray);
  184.                 }
  185.                 else {
  186.                     $fileArray[]=$rootPath.$seperator.$file;
  187.                 }
  188.             }
  189.         }      
  190.         return $fileArray;
  191.     }
  192.  
  193.     /**
  194.      * Function to Zip entire directory with all its files and subdirectories
  195.      *
  196.      * @param string $dirName
  197.      * @access public
  198.      * @return void
  199.      */
  200.     public function zipDirectory($dirName, $outputDir) {
  201.         if (!is_dir($dirName)){
  202.             trigger_error("CreateZipFile FATAL ERROR: Could not locate the specified directory $dirName", E_USER_ERROR);
  203.         }
  204.         $tmp=$this->parseDirectory($dirName);
  205.         $count=count($tmp);
  206.         $this->addDirectory($outputDir);
  207.         for ($i=0;$i<$count;$i++){
  208.             $fileToZip=trim($tmp[$i]);
  209.             $newOutputDir=substr($fileToZip,0,(strrpos($fileToZip,'/')+1));
  210.             $outputDir=$outputDir.$newOutputDir;
  211.             $fileContents=file_get_contents($fileToZip);
  212.             $this->addFile($fileContents,$fileToZip);
  213.         }
  214.     }
  215. }
  216. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement