file zip problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

file zip problem

Post by greedyisg00d »

I found this code on the net and I use it to zip a text file. The problem is when I try to extract the file is says an error "unexpected end of file". I tried to zip the text file manually and extract it afterwards and it works just fine. Any idea what seems to be the problem? Thanks

Code: Select all

echo"<p>Zip my Files</p>";  
     
   $files=array('fileA.txt','fileB.txt','fileC.txt');    
     
   if(create_zip($files, 'MYZIP.zip'))
   echo "Files Zipped Successfully";  
     
   function create_zip($files = array(),$destination = '',$overwrite = false)  
   {  
       if(file_exists($destination) && !$overwrite)  
       {  
           echo "Destination already exists";  
           return false;  
       }  
       $valid_files = array();    
     
       if(is_array($files))  
       {  
           foreach($files as $file)  
           {  
               if(file_exists($file))  
               {  
                   $valid_files[] = $file;  
               }  
           }  
       }    
     
       if(count($valid_files))  
       {  
           $zip = new ZipArchive();  
           if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)  
           {  
               echo "Can't zip files";  
               return false;  
           }    
     
           foreach($valid_files as $file)  
           {  
               $zip->addFile($file,$file);  
           }    
        echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status ,'<br>'; 
           $zip->close();    
     
          return file_exists($destination);  
       }  
       else  
       {  
           echo "No file found to zip";  
           return false;  
       }  
   } 
   
Post Reply