Created
April 26, 2010 19:42
-
-
Save gabrielgilini/379781 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Zip extends Object | |
| { | |
| private $_file; | |
| private $_errors = false; | |
| private $_dirName; | |
| private $_extracted = array(); | |
| public function __construct($path) | |
| { | |
| parent::__construct(); | |
| $this->_file = new File($path); | |
| if(!$this->_file->readable()) | |
| { | |
| $this->_errors = 1; | |
| } | |
| else | |
| { | |
| $out = $this->testFile(); | |
| if(stripos($out, "End-of-central-directory signature not found") !== false) | |
| { | |
| $this->_errors = 2; | |
| } | |
| elseif(stripos($out, "No errors detected") === false) | |
| { | |
| $this->_errors = 3; | |
| } | |
| } | |
| $this->_dirName = $this->_file->Folder->pwd(); | |
| } | |
| public function __destruct() | |
| { | |
| $this->_file->close(); | |
| } | |
| public function testFile() | |
| { | |
| exec("unzip -tq ".$this->_file->pwd(), $out); | |
| return join('', $out); | |
| } | |
| public function hasErrors() | |
| { | |
| return (bool)$this->_errors; | |
| } | |
| public function getErrorMessage() | |
| { | |
| switch($this->_errors) | |
| { | |
| case false: | |
| return false; | |
| case 1: | |
| return __('O arquivo não pôde ser aberto para leitura. Cheque as permissões.', true); | |
| case 2: | |
| return __('O arquivo não é do tipo ZIP.', true); | |
| default: | |
| return __('Ocorreu um erro ao abrir o arquivo.', true); | |
| } | |
| } | |
| public function extract($destination = null) | |
| { | |
| if(!$destination) | |
| { | |
| $destination = $this->_dirName; | |
| } | |
| exec("unzip -d $destination -o ".$this->_file->pwd(), $out, $ret); | |
| if($ret === 0) | |
| { | |
| array_shift($out); | |
| $this->buildFilenames($out, $this->_extracted); | |
| return true; | |
| } | |
| return false; | |
| } | |
| private function buildFilenames($outArr, &$extractedArr, $i = 0, $dirName = null) | |
| { | |
| for($j = count($outArr); $i < $j; ++$i) | |
| { | |
| if(($pos = stripos($outArr[$i], 'inflating: ')) !== false) | |
| { | |
| $filename = trim(substr($outArr[$i], 11+$pos)); | |
| if($dirName && stripos($filename, $dirName) === false) | |
| { | |
| return; | |
| } | |
| $extractedArr[] = $filename; | |
| } | |
| elseif(($pos = stripos($outArr[$i], 'creating: ')) !== false) | |
| { | |
| $dn = basename(trim(substr($outArr[$i], 10+$pos))); | |
| $extractedArr[$dn] = array(); | |
| $this->buildFilenames($outArr, $extractedArr[$dn], ++$i, $dn); | |
| } | |
| } | |
| } | |
| public function getFilenames() | |
| { | |
| return $this->_extracted; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gabriel, fiz as alterações que havia falado pra você, só não testei e acho que ainda não atende o que você precisa. Mas veja o que você acha ai, deve funcionar em PHP >= 5.2.0.