[size="6"]Zip Files Handler Using Yii[/size]
Create Zip Files Function
public function actionCreatefile($filename,$filelist)
{
$zip=new ZipArchive();
$destination=DIRDetails."/filename.zip";
if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($filelist as $thefile)
{
$random=rand(11111,99999);
$filename=$random.$thefile;
$zip->addFile($thefile->tempname,$filename);
}
$zip->close();
}
Update Zip Function
public function actionUpdatezip($zipfilename,$filelist){
$destination=Yii::app()->basePath.'/files/'.$zipfilename.".zip";
$zip=new ZipArchive();
if(!$zip->open($destination)) {
return false;
}
if($filelist)
{
foreach($filelist as $thefile)
{
$filemodel=new Filesmodle;
$randno=rand(11111,99999);
$filename=$randno.$thefile->name; // yii magic method
$zip->addFile($thefile->tempname,$filename);
//$fileext=$thefile->extensionName;
//$filemodel->Size=$thefile->size;
}
}
$zip->close();
}
Delete Files in Zip Function
public function actionDeletefile($zipfilename,$filename)
{
$error='';
$filepath=Yii::app()->basePath.'/files/'.$zipfilename.".zip";
$zip = new ZipArchive;
if ($zip->open($filepath) === TRUE) {
$zip->deleteName($filename); // this file of inside zip folder
} else {
$error='failed';
}
$zip->close();
}