[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 /** 4 * \defgroup File_Unpacker 5 * 6 * This module implements support for easily unpacking files compressed with 7 * .zip, .tar.gz, .tar.bz2 and .rar provided that all the binary tools are available. 8 * 9 * The main Unpacker class is a proxy class that will take care of finding the right 10 * subclass according to the package type. 11 */ 12 13 14 lt_include( PLOG_CLASS_PATH."class/file/unpacker/baseunpacker.class.php" ); 15 lt_include( PLOG_CLASS_PATH."class/file/unpacker/targzunpacker.class.php" ); 16 lt_include( PLOG_CLASS_PATH."class/file/unpacker/zipunpacker.class.php" ); 17 lt_include( PLOG_CLASS_PATH."class/file/unpacker/tarbz2unpacker.class.php" ); 18 lt_include( PLOG_CLASS_PATH."class/file/unpacker/rarunpacker.class.php" ); 19 20 define( "UNPACKER_AUTODETECT", "detect" ); 21 define( "UNPACKER_TAR_GZ", "tar.gz" ); 22 define( "UNPACKER_TAR_BZ2", "tar.bz2" ); 23 define( "UNPACKER_ZIP", "zip" ); 24 define( "UNPACKER_RAR", "rar" ); 25 define( "UNPACKER_UNSUPPORTED", false ); 26 27 /** 28 * \ingroup File_Unpacker 29 * 30 * Class that implements an object capable of unpacking several different 31 * kinds of compressed files. It will take care of finding the right unpacker class for the 32 * job, call it and return a result from it, so <b>there is no need to create instances of 33 * the other unpacker classes</b>. 34 * 35 * Example of usage: 36 * 37 * <pre> 38 * $unpacker = new Unpacker(); 39 * $unpacker->unpack( "/tmp/my_uploaded_file.tar.gz", "/tmp/results" ); 40 * </pre> 41 * 42 * More unpacker classes can be added if needed. 43 * 44 * @see BaseUnpacker 45 * @see TarGzUnpacker 46 * @see ZipUnpacker 47 * @see TarBz2Unpacker 48 * @see RarUnpacker 49 */ 50 class Unpacker 51 { 52 53 var $_methods = Array( "tar.gz" => "TarGzUnpacker", 54 "zip" => "ZipUnpacker", 55 "tar.bz2" => "TarBz2Unpacker", 56 "rar" => "RarUnpacker" 57 ); 58 59 var $_method; 60 61 var $_unpackerObj; 62 63 /** 64 * Creates an object of this class. The first parameter is the 65 * name of the file while the second parameter is the method 66 * we'd like to use. The class is able to to auto-detect 67 * the file we're using, but we can still force one specific 68 * unpacking method. 69 * 70 * @param method The method we'd like to use to unpack the file. It defaults to 71 * UNPACKER_AUTODETECT but it can also take any of the following values: 72 * <ul> 73 * <li>UNPACKER_TAR_GZ</li> 74 * <li>UNPACKER_TAR_GZ2</li> 75 * <li>UNPACKER_ZIP</li> 76 * <li>UNPACKER_RAR</li> 77 * </ul> 78 */ 79 function Unpacker( $method = UNPACKER_AUTODETECT ) 80 { 81 82 83 $this->_method = $method; 84 } 85 86 /** 87 * finds the right unpacker class 88 * @private 89 */ 90 function _findUnpacker() 91 { 92 if( $this->_method == UNPACKER_AUTODETECT ) { 93 $extArray = explode( ".", $this->_file ); 94 95 $ext = $extArray[count($extArray)-1]; 96 $ext2 = $extArray[count($extArray)-2].".".$ext; 97 98 if( isset($this->_methods[$ext])) 99 $this->_method = $ext; 100 elseif( isset($this->_methods[$ext2])) 101 $this->_method = $ext2; 102 else 103 $this->_method = UNPACKER_UNSUPPORTED; 104 } 105 106 // create the object 107 if( $this->_method != UNPACKER_UNSUPPORTED ) { 108 $this->_unpacker = new $this->_methods[$this->_method](); 109 $result = true; 110 } 111 else 112 $result = false; 113 114 return $result; 115 } 116 117 /** 118 * Unpacks the file using the selected method to the destination 119 * folder. The method used to unpack the file will be autodetected from the file type 120 * unless the parameter to the autoconstructor is different from UNPACKER_AUTODETECT 121 * 122 * @param file path to the file that we're going to unpack 123 * @param destFolder destination folder where files should be unpacked 124 * @return UNPACKER_UNSUPPORTED if the file cannot be unpacked with any of the known unpacker 125 * classe, true if successful or false if not. 126 */ 127 function unpack( $file, $destFolder = "./" ) 128 { 129 // find the most suitable unpacker mechanism 130 $this->_file = $file; 131 132 if( !$this->_findUnpacker()) 133 return UNPACKER_UNSUPPORTED; 134 135 // and then just do it 136 return $this->_unpacker->unpack( $file, $destFolder ); 137 } 138 } 139 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |