Yii Zip Format

In this article, I explained about "How to handle zip files" in yii framework. I used this code to my project for handling the zip files. Here i added source code to create, Extract, Download and Delete zip files in yii framework.

Create Zip Files in Yii

<?php
public function actionCreateZip(){
$zip=new ZipArchive();
$destination=DIRDetails."/filename.zip";
if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) {
   return false;
}
$doclist=CUploadedFile::getInstancesByName('files');
foreach($doclist as $thefile)
{
   $random=rand(11111,99999);
   $filename=$random.$thefile;      
   $zip->addFile($thefile->tempname,$filename);
}
$zip->close();   
}
?>

Extract Zip Files in Yii

<?php
public function actionExtractZip(){
$filename='filename';     
$zipfile=DirDetails."/filename.zip";   
$zip = zip_open($zipfile);
$extract=DirDetails."/newfolder";        
if ($zip) 
{    
    if(!is_dir($extract))
         mkdir($extract);  
    while ($zip_entry = zip_read($zip)) {
 // if(zip_entry_name($zip_entry)==$filename)
 //if you need any specified file use this condition
    {                    
      $fp = fopen($extract."/".zip_entry_name($zip_entry), "w");                
      if (zip_entry_open($zip, $zip_entry, "r")) {
         $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
         fwrite($fp,"$buf");
         zip_entry_close($zip_entry);
         fclose($fp);
         break;
      }
     }
    }
   zip_close($zip);
  }
}
?>

Download Zip Files in Yii

<?php
public function downloadfinal($docid,$docname)
    {
        $filename=Dirdetails"filename.zip"; 
        if(file_exists($filename)){
            $path_parts = pathinfo($filename); 
            $ext = strtolower($path_parts["extension"]); 
            switch ($ext) 
            { 
              case "pdf": $ctype="application/pdf"; break; 
              case "exe": $ctype="application/octet-stream"; break; 
              case "zip": $ctype="application/zip"; break; 
              case "doc": $ctype="application/msword"; break; 
              case "xls": $ctype="application/vnd.ms-excel"; break; 
              case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
              case "gif": $ctype="image/gif"; break; 
              case "png": $ctype="image/png"; break; 
              case "jpg": $ctype="image/jpg"; break; 
              default: $ctype="application/force-download"; 
            } 
            $newpath=$extract.'/'.$filename;    
            header("Pragma: public");
            header("Content-Type: application/$ctype");
            header("Content-Disposition: inline; filename=$docname");
            header('Content-Length: '.filesize($filename));
            header("Accept Ranges: bytes");
            header("Expires: 0"); 
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
            readfile("$filename");
        }
       else
       {
        echo "error";
       }
   }
?>

Delete Zip Files in Yii

<?php
public function actionDeletefile($id,$docid)
{
    $docpath=Yii::app()->basePath.'/files/livefiles/'.$docid.".zip";   
    $zip = new ZipArchive;
    if ($zip->open($docpath) === TRUE) {
        $zip->deleteName($dfilename);
        $update=Files::model()->updateAll(array( 'IsActive' => 0 ), 'FileID ='.$id);
      
        if($count==1){
           $updatecount=Documenttable::model()->updateAll(array( 'IsDesired' => 1 ), 'DocumentID='.$docid);
        }
    } else {
        $error='failed';
    }
    $zip->close();  
           
    }
    else
    {
        $error="All Files Are Deletee ";
    }

}
?>