Jump to content

Suggestions needed on unzip utility class


anupamsaha

Recommended Posts

Hello,

 

Is there any suggestion in improving the class below?

 

<?php
/*
* Zipper class
* Basically a wrapper class of ZipArchive
* Requires zlib extension with PHP
* And, command line unzipping utility
* 
* Usage:
* try {
* 		$zip = new Zipper('commandline');
* 		$zip->setZipFile('filename.zip');
* 		$zip->setUnzipPath('/www/myfolder');
* 		$zip->unzip();
* }
* catch (Exception $e) {
* 		print $e->getMessage();
* }
* 
* @author $Author$
* @version $Id$
*/
class Zipper {
/**
 * ZipArchive Class Instance holder
 * @var mixed
 */
private $_zip = false;

/**
 * Unzip type (commandline|zlib)
 * @var array
 */
private $_unzipList = array(
	'commandline', 
	'zlib'
);

/**
 * Current unzip type
 * @var string
 */
private $_currentUnzipType = false;

/**
 * ZIP filename
 * @var mixed
 */
private $_zipFile = false;

/**
 * Unzip location
 * @var mixed
 */
private $_unzipLocation = './';

/**
 * Magic constructor
 * 
 * @access public
 * @param string $zipFile
 * @return void
 * @throws Exception
 */
public function __construct($unzipType) {
	$this->setUnzipType($unzipType);
}

/**
 * Check zlib extension available or not
 * If available, create an instance of the same
 * 
 * @access private
 * @param void
 * @return void
 * @throws Exception
 */
private function _getZlibInstance() {
	// If zlib extension is not installed, throw an Exception
	if (!class_exists('ZipArchive')) {
		throw new Exception('Class ZipArchive does not exist');
	}

	// Create instance to ZipArchive class
	$this->_zip = new ZipArchive();
}

/**
 * Set unzip type (through system command or zib)
 * 
 * @access public
 * @param string $unzipType
 * @return void
 */
public function setUnzipType($unzipType) {
	if (empty($unzipType)) {
		throw new Exception('Unzip type not specified');
	}
	if (!in_array(strtolower($unzipType), $this->_unzipList)) {
		throw new Exception('Unrecognized unzip type');
	}
	$this->_currentUnzipType = strtolower($unzipType);

	if ($this->_currentUnzipType == 'zlib') {
		$this->_getZlibInstance();
	}
}

/**
 * Set ZIP Filename with or without location
 * 
 * @access public
 * @param string $zipFile
 * @return void
 * @throws Exception
 */
public function setZipFile($zipFile = null) {
	if (is_null($zipFile)) {
		throw new Exception('No zip file specified');
	}
	if (!file_exists($zipFile)) {
		throw new Exception('File \'' . $zipFile . '\' does not exist');
	}
	$this->_zipFile = $zipFile;
}

/**
 * Get ZIP Filename
 * 
 * @access public
 * @param void
 * @return mixed
 */
public function getZipFile() {
	return $this->_zipFile;
}

/**
 * Set Unzip path
 * 
 * @access public
 * @param string $unzipPath
 * @return void
 * @throws Exception
 */
public function setUnzipPath($unzipPath = null) {
	if (!is_null($unzipPath)) {
		if (!$this->_isDirectory($unzipPath)) {
			throw new Exception("$path is not a directory");
		}
		else if (!$this->_isWritable($unzipPath)) {
			throw new Exception("$path is not writable");
		}
		else {
			$this->_unzipLocation = $unzipPath;
		}
	}
}

/**
 * Get Unzip path
 * 
 * @access public
 * @param void
 * @return mixed
 */
public function getUnzipPath() {
	return $this->_unzipLocation;
}

/**
 * Check whether a path denotes to a directory or not
 * 
 * @access private
 * @param string $path
 * @return boolean
 */
private function _isDirectory($path) {
	return is_dir($path);
}

/**
 * Checks whether a path specified is writable or not
 * 
 * @access private
 * @param string $path
 * @return boolean
 */
private function _isWritable($path) {
	return is_writable($path);
}

/**
 * Unzip a file
 * 
 * @access public
 * @param void
 * @return mixed
 * @throws Exception
 */
public function unzip() {
	if (!$this->_zipFile) {
		throw new Exception('Zip filename not specified');
	}

	switch($this->_currentUnzipType) {
		case 'zlib':
			return $this->_processZlib();
			break;
		case 'commandline':
			return $this->_processCommandline();
			break;
		default:
			throw new Exception('Unrecognized unzip method');
	}
}

/**
 * Unzipping through Zlib
 * 
 * @access private
 * @param void
 * @return boolean
 * @throws Exception
 */
private function _processZlib() {
	if (!$this->_zip) {
		$this->_getZlibInstance();
	}
	if ($this->_zip->open($this->_zipFile) === FALSE) {
		throw new Exception('Unable to open \'' . $this->_zipFile . '\'');
	}
	return $this->_zip->extractTo($this->_unzipLocation);		
}

/**
 * Unzipping through Commandline
 * 
 * @access private
 * @param void
 * @return boolean
 * @throws Exception
 */
private function _processCommandline() {
	$result = system(
		'unzip ' . 
		escapeshellcmd($this->_zipFile) . 
		' -d ' . 
		escapeshellcmd($this->_unzipLocation)
	);
	if ($result === FALSE) {
		throw new Exception('Unzip process failed');
	}
	return $result;
}
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.