Zip File Handling In Yii

[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();                 

}

There is already ZIP support in PHP - http://php.net/manual/en/book.zip.php

Hello

In the method "CreateFile ($filename, $filelist)":

The parameter "$filelist" is an array of that type?

The parameter "$filename" seems unused, overwritten???

I tried this:

$filelist[0] = Yii::app()->file->set(‘one.txt’, true);

$filelist[1] = Yii::app()->file->set(‘two.txt’, true);

$this -> actionCreatefile("zipfile.zip", $filelist);

but it does not work, I get error here:

$filename = $random.$thefile;

if replacement by

$filename = $random.$thefile->filename;

and then gives me error:

$zip->addFile($thefile->tempname, $filename);

I have no property "tempname".

All this leads me to believe that $thefile is not an instance of CFile.

Also try this:

Yii::import('application.extensions.file.CFileHelper", true);

$cf_file = CFileHelper::get(‘one.txt’);

But I get the following error:

Fatal error: Can not redeclare class CApplicationComponent in C:\xampp\htdocs\crcsoftnews\public_html\protected\extensions\file\CFileHelper.php on line 12

Any idea?

thanks