/** * @param string The name of the archive file * @param string Directory to unpack into * @return boolean True for success */ function extract($archivename, $extractdir) { require_once dirname(__FILE__) . '/file.php'; require_once dirname(__FILE__) . '/folder.php'; $untar = false; $result = false; $ext = extFile::getExt(strtolower($archivename)); // check if a tar is embedded...gzip/bzip2 can just be plain files! if (extFile::getExt(extFile::stripExt(strtolower($archivename))) == 'tar') { $untar = true; } switch ($ext) { case 'zip': $adapter =& extArchive::getAdapter('zip'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tar': $adapter =& extArchive::getAdapter('tar'); if ($adapter) { $result = $adapter->extract($archivename, $extractdir); } break; case 'tgz': $untar = true; // This format is a tarball gzip'd // This format is a tarball gzip'd case 'gz': // This may just be an individual file (e.g. sql script) // This may just be an individual file (e.g. sql script) case 'gzip': $adapter =& extArchive::getAdapter('gzip'); if ($adapter) { $tmpfname = (defined('_EXT_FTPTMP_PATH') ? _EXT_FTPTMP_PATH . '/' : $extractdir) . uniqid('gzip'); $gzresult = $adapter->extract($archivename, $tmpfname); if (!$gzresult) { unlink($tmpfname); return false; } if ($untar) { // Try to untar the file $tadapter =& extArchive::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = extPath::clean($extractdir); mkdir($path); $result = copy($tmpfname, $path . DS . extFile::stripExt(extFile::getName(strtolower($archivename)))); } @unlink($tmpfname); } break; case 'tbz2': $untar = true; // This format is a tarball bzip2'd // This format is a tarball bzip2'd case 'bz2': // This may just be an individual file (e.g. sql script) // This may just be an individual file (e.g. sql script) case 'bzip2': $adapter =& extArchive::getAdapter('bzip2'); if ($adapter) { $tmpfname = _EXT_FTPTMP_PATH . '/' . uniqid('bzip2'); $bzresult = $adapter->extract($archivename, $tmpfname); if (!$bzresult) { @unlink($tmpfname); return false; } if ($untar) { // Try to untar the file $tadapter =& extArchive::getAdapter('tar'); if ($tadapter) { $result = $tadapter->extract($tmpfname, $extractdir); } } else { $path = extPath::clean($extractdir); mkdir($path); $result = copy($tmpfname, $path . DS . extFile::stripExt(extFile::getName(strtolower($archivename)))); } @unlink($tmpfname); } break; default: return PEAR::raiseError('Unknown Archive Type: ' . $ext); break; } return $result; }
/** * @param string The name of the archive * @param mixed The name of a single file or an array of files * @param string The compression for the archive * @param string Path to add within the archive * @param string Path to remove within the archive * @param boolean Automatically append the extension for the archive * @param boolean Remove for source files */ function create($archive, $files, $compress = 'tar', $addPath = '', $removePath = '', $autoExt = false) { $compress = strtolower($compress); if ($compress == 'tgz' || $compress == 'tbz' || $compress == 'tar') { require_once _EXT_PATH . '/libraries/Tar.php'; if (is_string($files)) { $files = array($files); } if ($autoExt) { $archive .= '.' . $compress; } if ($compress == 'tgz') { $compress = 'gz'; } if ($compress == 'tbz') { $compress = 'bz2'; } $tar = new Archive_Tar($archive, $compress); $tar->setErrorHandling(PEAR_ERROR_PRINT); $result = $tar->addModify($files, $addPath, $removePath); return $result; } elseif ($compress == 'zip') { $adapter =& extArchive::getAdapter('zip'); if ($adapter) { $result = $adapter->create($archive, $files, array('remove_path' => $removePath)); } if ($result == false) { return PEAR::raiseError('Unrecoverable ZIP Error'); } } }