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 /* $Id: InfoZipArchive.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     final class InfoZipArchive extends FileArchive
00019     {
00020         private $zipArchive = null;
00021 
00022         public function __construct($cmdBinPath = '/usr/bin/unzip')
00023         {
00024             $usingCmd = $cmdBinPath;
00025 
00026             if (class_exists('ZipArchive', false)) {
00027 
00028                 $this->zipArchive = new ZipArchive();
00029                 $usingCmd = null;
00030 
00031             } elseif ($usingCmd === null)
00032                 throw
00033                     new UnsupportedMethodException(
00034                         'no built-in support for zip'
00035                     );
00036 
00037             parent::__construct($usingCmd);
00038         }
00039 
00040         public function open($sourceFile)
00041         {
00042             parent::open($sourceFile);
00043 
00044             if ($this->zipArchive) {
00045                 $resultCode = $this->zipArchive->open($sourceFile);
00046                 
00047                 if ($resultCode !== true)
00048                     throw new ArchiverException(
00049                         'ZipArchive::open() returns error code == '.$resultCode
00050                     );
00051             }
00052 
00053             return $this;
00054         }
00055 
00056         public function readFile($fileName)
00057         {
00058             if (!$this->sourceFile)
00059                 throw
00060                     new WrongStateException(
00061                         'dude, open an archive first.'
00062                     );
00063             
00064             if ($this->zipArchive) {
00065                 $result = $this->zipArchive->getFromName($fileName);
00066 
00067                 if ($result === false)
00068                     throw new ArchiverException(
00069                         'ZipArchive::getFromName() failed'
00070                     );
00071                 
00072                 return $result;
00073             }
00074 
00075             $options = '-c -q'
00076                 .' '.escapeshellarg($this->sourceFile)
00077                 .' '.escapeshellarg($fileName);
00078 
00079             return $this->execStdoutOptions($options);
00080         }
00081     }
00082 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4