Advertisement
shaked

PHP ZipArchive Exmaple

Dec 17th, 2011
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. <?php
  2. /**
  3.  * ZipArchiveHelper class
  4. */
  5. class ZipArchiveHelper {
  6.     /**
  7.      * @var ZipArchive $_zipArchive
  8.     */
  9.     private $_zipArchive;
  10.     /**
  11.      * @var array $_excludeList
  12.     */
  13.     private $_excludeList;
  14.  
  15.     const TIMEOUT = 5000;
  16.  
  17.     /**
  18.      * Get files list by path
  19.      * @param string $sourcePath
  20.      * @return RecursiveIteratorIterator
  21.     */
  22.     private function _getFilesList($sourcePath){
  23.         $dirlist = new RecursiveDirectoryIterator($sourcePath);
  24.         $filelist = new RecursiveIteratorIterator($dirlist);
  25.    
  26.         return $filelist;
  27.     }
  28.  
  29.     /**
  30.      * Create Zip File
  31.      * @param string $zipFileName
  32.      * @param string $sourcePath
  33.      * @param array[optional] $excludeList
  34.      * @return string|false
  35.     */
  36.     public function createZipFile($zipFileName,$sourcePath,array $excludeList = array()){
  37.             //Set PHP timeout
  38.             ini_set('max_execution_time', self::TIMEOUT);
  39.  
  40.             //set exclude list
  41.             $this->_excludeList = $excludeList;
  42.  
  43.             //set new zip archive object
  44.             $this->_setZipArchive();
  45.  
  46.             //generate new file
  47.             $this->_newZipFile($zipFileName);
  48.        
  49.             //compress
  50.             $this->_compress($sourcePath);
  51.  
  52.             //get status
  53.             $retValue = $this->_zipArchive->getStatusString();
  54.  
  55.             //close zip archive manager
  56.             $this->_zipArchive->close();
  57.  
  58.             return $retValue;
  59.     }
  60.  
  61.     /**
  62.      * Set new ZipArchive instance
  63.      * Seperated and protected for future unit tests
  64.      * @return void
  65.     */
  66.     protected function _setZipArchive(){
  67.         $this->_zipArchive = new ZipArchive;
  68.     }
  69.  
  70.     /**
  71.      * Open new|existing zip file
  72.      * @param int $type -   Overwrite by default. (you may use other options, e.g ZipArchive::CREATE)
  73.      * @throw Exception
  74.      * @return void
  75.     */
  76.     protected function _newZipFile($zipFileName,$type=ZipArchive::OVERWRITE){
  77.         if ($this->_zipArchive->open("$zipFileName", $type) !== TRUE) {
  78.                 throw new Exception("Could not open archive");
  79.         }  
  80.     }
  81.  
  82.     /**
  83.      * Validate file by checking if exists and not excluded type
  84.      * @param string $fileName
  85.      * @return bool
  86.     */
  87.     private function _isValidFile($fileName){
  88.         if (!is_file($fileName)){
  89.             return false ;
  90.         }
  91.  
  92.         foreach($this->_excludeList as $toExclude){
  93.             if (strpos($fileName,$toExclude) !== false){
  94.                 return false;
  95.             }
  96.         }  
  97.  
  98.         return true;
  99.     }
  100.  
  101.     /**
  102.      * Compress to ZIP file
  103.      * @return bool
  104.     */
  105.     private function _compress($sourcePath){
  106.         //get file list        
  107.         $filelist = $this->_getFilesList($sourcePath);
  108.  
  109.         //add files to new zip file
  110.         foreach ($filelist as $fileName=>$value) {
  111.             if(!$this->_isValidFile($fileName)){
  112.                 continue ;
  113.             }
  114.            
  115.             //you should use sprintf for this to work on both *NIX and Windows OS
  116.             //note that str_replace is just for my example, you don't really have to use it.
  117.             $value = sprintf("%s",str_replace($sourcePath,'',$value));
  118.  
  119.             if (!$this->_zipArchive->addFile($fileName,$value)){
  120.                  throw new Exception("ERROR: Could not add file: $fileName");
  121.             }
  122.         }
  123.  
  124.         return true;
  125.     }
  126. }
  127. //PHP >= 5.3 just throws this annoying warning
  128. date_default_timezone_set('Asia/Jerusalem');
  129.  
  130. $zipHelper = new ZipArchiveHelper();
  131. try {
  132.     var_dump($zipHelper->createZipFile(
  133.         'shakedos.zip',
  134.         '/tmp/zend_debug',
  135.         array('.svn')
  136.     ));
  137. } catch (Exception $e){
  138.     var_dump($e->getMessage());
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement