InfoZipArchive.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00019     final class InfoZipArchive extends FileArchive
00020     {
00021         private $zipArchive = null;
00022         
00023         public function __construct($cmdBinPath = '/usr/bin/unzip')
00024         {
00025             $usingCmd = $cmdBinPath;
00026             
00027             if (class_exists('ZipArchive', false)) {
00028                 
00029                 $this->zipArchive = new ZipArchive();
00030                 $usingCmd = null;
00031                 
00032             } elseif ($usingCmd === null)
00033                 throw
00034                     new UnsupportedMethodException(
00035                         'no built-in support for zip'
00036                     );
00037             
00038             parent::__construct($usingCmd);
00039         }
00040         
00041         public function open($sourceFile)
00042         {
00043             parent::open($sourceFile);
00044             
00045             if ($this->zipArchive) {
00046                 $resultCode = $this->zipArchive->open($sourceFile);
00047                 
00048                 if ($resultCode !== true)
00049                     throw new ArchiverException(
00050                         'ZipArchive::open() returns error code == '.$resultCode
00051                     );
00052             }
00053             
00054             return $this;
00055         }
00056         
00057         public function readFile($fileName)
00058         {
00059             if (!$this->sourceFile)
00060                 throw
00061                     new WrongStateException(
00062                         'dude, open an archive first.'
00063                     );
00064             
00065             if ($this->zipArchive) {
00066                 $result = $this->zipArchive->getFromName($fileName);
00067                 
00068                 if ($result === false)
00069                     throw new ArchiverException(
00070                         'ZipArchive::getFromName() failed'
00071                     );
00072                 
00073                 return $result;
00074             }
00075             
00076             $options = '-c -q'
00077                 .' '.escapeshellarg($this->sourceFile)
00078                 .' '.escapeshellarg($fileName);
00079             
00080             return $this->execStdoutOptions($options);
00081         }
00082     }
00083 ?>