Changeset 443

Show
Ignore:
Timestamp:
06/12/09 13:51:44 (9 months ago)
Author:
mrook
Message:

Rewrite UnzipTask using PHP zip library (patch by George Miroshnikov)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/2.4/classes/phing/tasks/ext/UnzipTask.php

    r144 r443  
    2121require_once 'phing/tasks/ext/ExtractBaseTask.php'; 
    2222require_once 'phing/system/io/FileSystem.php'; 
    23 require_once 'phing/lib/Zip.php'; 
    2423 
    2524/** 
    26  * Extracts one or several zip archive using PEAR Archive_Zip (which is presently unreleased 
    27  * and included with Phing). 
     25 * Extracts one or several zip archives using ZipArchive class. 
    2826 * 
    29  * @author    Joakim Bodin <joakim.bodin+phing@gmail.com> 
    30  * @version   $Revision: 1.0 $ 
    31  * @package   phing.tasks.ext 
    32  * @since     2.2.0 
     27 * @author  Joakim Bodin <joakim.bodin+phing@gmail.com> 
     28 * @author  George Miroshnikov <laggy.luke@gmail.com> 
     29 * @version $Revision$ 
     30 * @package phing.tasks.ext 
    3331 */ 
    34 class UnzipTask extends ExtractBaseTask { 
    35      
     32class UnzipTask extends ExtractBaseTask 
     33{ 
     34    /** 
     35     * Extract archive content into $this->todir directory 
     36     * @param PhingFile Zip file to extract 
     37     * @return boolean 
     38     */ 
    3639    protected function extractArchive(PhingFile $zipfile) 
    3740    { 
    38         $extractParams = array('add_path' => $this->todir->getAbsolutePath()); 
    39         if(!empty($this->removepath)) 
    40         { 
    41             $extractParams['remove_path'] = $this->removepath; 
     41        $this->log("Extracting zip: " . $zipfile->__toString() . ' to ' . $this->todir->__toString(), Project::MSG_INFO); 
     42         
     43        $zip = new ZipArchive(); 
     44         
     45        $result = $zip->open($zipfile->getAbsolutePath()); 
     46        if (!$result) { 
     47            $this->log("Unable to open zipfile " . $zipfile->__toString(), Project::MSG_ERR); 
     48            return false; 
    4249        } 
    4350         
    44         $this->log("Extracting zip: " . $zipfile->__toString() . ' to ' . $this->todir->__toString(), Project::MSG_INFO); 
     51        $result = $zip->extractTo($this->todir->getAbsolutePath()); 
     52        if (!$result) { 
     53            $this->log("Unable to extract zipfile " . $zipfile->__toString(), Project::MSG_ERR); 
     54            return false; 
     55        } 
    4556         
    46         try { 
    47                 $zip = new Archive_Zip($zipfile->getAbsolutePath()); 
    48                  
    49                 $extractResponse = $zip->extract($extractParams); 
    50                 if(is_array($extractResponse)) { 
    51                     foreach ($extractResponse as $extractedPath) { 
    52                         $this->log('Extracted' . $extractedPath['stored_filename'] . ' to ' . $this->todir->__toString(), Project::MSG_VERBOSE); 
    53                     } 
    54                 } else if ($extractResponse === 0) { 
    55                     throw new BuildException('Failed to extract zipfile: ' . $zip->errorInfo(true)); 
    56                 } 
    57         } catch (IOException $ioe) { 
    58             $msg = "Could not extract ZIP: " . $ioe->getMessage(); 
    59             throw new BuildException($msg, $ioe, $this->getLocation()); 
    60         } 
     57        return true; 
    6158    } 
    6259     
     60    /** 
     61     * List archive content 
     62     * @param PhingFile Zip file to list content 
     63     * @return array List of files inside $zipfile 
     64     */ 
    6365    protected function listArchiveContent(PhingFile $zipfile) 
    6466    { 
    65         $zip = new Archive_Zip($zipfile->getAbsolutePath()); 
    66         return $zip->listContent(); 
     67        $zip = new ZipArchive(); 
     68        $zip->open($zipfile->getAbsolutePath()); 
     69         
     70        $content = array(); 
     71        for ($i = 0; $i < $zip->numFiles; $i++) { 
     72            $content[] = $zip->getNameIndex($i); 
     73        } 
     74        return $content; 
    6775    } 
    6876} 
     77