Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * ZipArchiveHelper class
- */
- class ZipArchiveHelper {
- /**
- * @var ZipArchive $_zipArchive
- */
- private $_zipArchive;
- /**
- * @var array $_excludeList
- */
- private $_excludeList;
- const TIMEOUT = 5000;
- /**
- * Get files list by path
- * @param string $sourcePath
- * @return RecursiveIteratorIterator
- */
- private function _getFilesList($sourcePath){
- $dirlist = new RecursiveDirectoryIterator($sourcePath);
- $filelist = new RecursiveIteratorIterator($dirlist);
- return $filelist;
- }
- /**
- * Create Zip File
- * @param string $zipFileName
- * @param string $sourcePath
- * @param array[optional] $excludeList
- * @return string|false
- */
- public function createZipFile($zipFileName,$sourcePath,array $excludeList = array()){
- //Set PHP timeout
- ini_set('max_execution_time', self::TIMEOUT);
- //set exclude list
- $this->_excludeList = $excludeList;
- //set new zip archive object
- $this->_setZipArchive();
- //generate new file
- $this->_newZipFile($zipFileName);
- //compress
- $this->_compress($sourcePath);
- //get status
- $retValue = $this->_zipArchive->getStatusString();
- //close zip archive manager
- $this->_zipArchive->close();
- return $retValue;
- }
- /**
- * Set new ZipArchive instance
- * Seperated and protected for future unit tests
- * @return void
- */
- protected function _setZipArchive(){
- $this->_zipArchive = new ZipArchive;
- }
- /**
- * Open new|existing zip file
- * @param int $type - Overwrite by default. (you may use other options, e.g ZipArchive::CREATE)
- * @throw Exception
- * @return void
- */
- protected function _newZipFile($zipFileName,$type=ZipArchive::OVERWRITE){
- if ($this->_zipArchive->open("$zipFileName", $type) !== TRUE) {
- throw new Exception("Could not open archive");
- }
- }
- /**
- * Validate file by checking if exists and not excluded type
- * @param string $fileName
- * @return bool
- */
- private function _isValidFile($fileName){
- if (!is_file($fileName)){
- return false ;
- }
- foreach($this->_excludeList as $toExclude){
- if (strpos($fileName,$toExclude) !== false){
- return false;
- }
- }
- return true;
- }
- /**
- * Compress to ZIP file
- * @return bool
- */
- private function _compress($sourcePath){
- //get file list
- $filelist = $this->_getFilesList($sourcePath);
- //add files to new zip file
- foreach ($filelist as $fileName=>$value) {
- if(!$this->_isValidFile($fileName)){
- continue ;
- }
- //you should use sprintf for this to work on both *NIX and Windows OS
- //note that str_replace is just for my example, you don't really have to use it.
- $value = sprintf("%s",str_replace($sourcePath,'',$value));
- if (!$this->_zipArchive->addFile($fileName,$value)){
- throw new Exception("ERROR: Could not add file: $fileName");
- }
- }
- return true;
- }
- }
- //PHP >= 5.3 just throws this annoying warning
- date_default_timezone_set('Asia/Jerusalem');
- $zipHelper = new ZipArchiveHelper();
- try {
- var_dump($zipHelper->createZipFile(
- 'shakedos.zip',
- '/tmp/zend_debug',
- array('.svn')
- ));
- } catch (Exception $e){
- var_dump($e->getMessage());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement