<?php
class FolderToZipConverter
{
private $_isRecursiveVar = false;
private $_foldersList = array();
private $_zipFileFilePath = ”;
private $_zipFile;
/**
* Constructor
*/
function __construct() {
$this->_zipFile = new ZipArchive();
}
/**
* Sets Recursive Behaviour in a folder
* @param boolean $isRecursiveVar Indicates if folders will be scaned recursively or not
*/
function setRecursiveness($isRecursiveVar)
{
$this->_isRecursiveVar = $isRecursiveVar;
}
/**
* Adds folder to be included into the Zip Archive
* @param array $foldersList Array of Folder Paths
*/
function addFolders(array $foldersList)
{
$this->_foldersList = array_merge($this->_foldersList, $foldersList);
}
/**
* Sets Zip file Name with its Path
* @param string $path Path of the zip with its name
* @return array
*/
function setZipFilePath($path)
{
if ($this->_zipFile->open($path, ZipArchive::CREATE)!==TRUE) {
return array(“error”=>true, “msg”=>”Can not open or create <$path>”);
} else {
$this->_zipFileFilePath = $path;
return array(“successfull”=>true);
}
}
/**
* Creates Zip Archive from your Provided Folders
* @return array
*/
function createArchiveFile()
{
if (count($this->_foldersList) == 0) {
return array(“error”=>true, “msg”=>”You did not set Folder(s) which needs to be added into Zip Archive”);
}
if ($this->_zipFileFilePath == ”) {
return array(“error”=>true, “msg”=>”Please set Zip Path first before Conversion process.”);
}
ini_set(‘memory_limit’, ‘-1’);
foreach ($this->_foldersList as $folder) {
$parent = substr($folder, (strlen(dirname($folder))+1)-strlen($folder));
if ($parent==$folder) {
$parent = ”;
} else {
$this->_zipFile->addEmptyDir($parent);
}
$result = $this->_scanDir($folder, $parent);
if (is_array($result) && isset($result[‘error’])) {
return $result;
}
}
$msg[“Num_files”] = $this->_zipFile->numFiles;
$this->_zipFile->close();
return array(“successfull”=>true, “statistics”=>$msg);
}
/**
* Scans Folder Recursively
* @param string $dir Directory to be Scaned
* @param string $parentDir Parent Directory
* @return array | boolean
*/
private function _scanDir($dir, $parentDir=”)
{
$cdir = array_diff(scandir($dir), array(‘..’, ‘.’));
foreach ($cdir as $key => $value) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
if ($this->_isRecursiveVar) {
if ($parentDir==”) {
$newDir = $value;
} else {
$newDir = $parentDir. DIRECTORY_SEPARATOR . $value;
}
if($this->_zipFile->addEmptyDir($newDir)) {
$this->_scanDir($dir . DIRECTORY_SEPARATOR . $value, $newDir);
} else {
$this->_zipFile->close();
return array(
“error”=>true,
“msg”=>”Could not create <$parentDir”. DIRECTORY_SEPARATOR . “$value> folder in Zip Archive.”
);
}
}
} elseif (is_file($dir . DIRECTORY_SEPARATOR . $value)) {
if ($parentDir==”) {
$newDir = $value;
$newName = ”;
} else {
$newDir = $dir. DIRECTORY_SEPARATOR . $value;
$newName = $parentDir. DIRECTORY_SEPARATOR . $value;
}
$result = $this->_zipFile->addFile($newDir, $newName);
}
}
return true;
}
}
?>
/***Example for using the class****/
<?php
$zip = new FolderToZipConverter();
$zip->setRecursiveness(true);
$zip->addFolders(
array(
‘./’, //path of the current file
‘F:\wamp2\www\yml’, //Windows Path
‘/var/www/html’ //linux path
)
);
$zip->setZipFilePath(‘./zip_files.zip’);
$result = $zip->createArchiveFile();
echo var_dump($result);
?>
- Jquery webcam plugin - June 19, 2016
- How To Add and Delete Users on a CentOSServer - June 5, 2016
- How To Set Up vsftpd on CentOS 6 - June 5, 2016