PHP Classes

Little "Addon"

Recommend this page to a friend!

      Create ZIP File  >  All threads  >  Little "Addon"  >  (Un) Subscribe thread alerts  
Subject:Little "Addon"
Summary:Two functions i wrote, that make createZip's usage very easy
Messages:2
Author:Rosen
Date:2009-01-25 11:48:18
Update:2009-01-25 12:04:12
 

  1. Little "Addon"   Reply   Report abuse  
Picture of Rosen Rosen - 2009-01-25 11:48:18
Just add these functions in the createZip class.

<------------ [CODE] ------------>
public function zipDir($dir, $mainObj = ''){

if( ($mainObj != '') && (substr($mainObj, -1) != '/') ) {
$mainObj .= '/';
}

if( ($dir != null) && (substr($dir, -1) == '/') ) {
$dir = substr($dir, 0, (strlen($dir)-2) ); // I don't know why i did this :D
}

$this -> addDirectory($mainObj);
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if($entry != '.' && $entry != '..' && $entry != 'Thumbs.db'){
if(is_dir($dir.'/'.$entry)){
$this->zipDir($dir.'/'.$entry, $mainObj.$entry);
#echo '<b>DIR: '.$dir.'/'.$entry.'</b><br>';
}else{
$fileContents = file_get_contents($dir.'/'.$entry);
$this -> addFile($fileContents, $mainObj.$entry);
#echo '<b>FILE: '.$dir.'/'.$entry.'</b><br>';
}
}
}
$d->close();

}

public function zipFiles($zipList){

foreach($zipList as $toZip){
if(is_dir($toZip)){
$this->zipDir($toZip);
}else{
$fileContents = file_get_contents($toZip);
$this -> addFile($fileContents, $toZip);
}
}

}


<------------ [/CODE] ------------>


The usage is simple. Just do this:


<------------ [CODE] ------------>

$zipFiles = array('dir1', 'dir2', 'file1', 'dir3', 'file2');
$createZip -> zipFiles($zipFiles);

<------------ [/CODE] ------------>


Use the full path of the files, e.g. $_SERVER['DOCUMENT_ROOT'].'/www/myDirToZip', or just the relative, e.g. '/www/myDirToZip'.
Don't use links like this '../../myDirToZip'.

  2. Re: Little "Addon"   Reply   Report abuse  
Picture of Rosen Rosen - 2009-01-25 12:04:12 - In reply to message 1 from Rosen
It didn't work well... So i fixed it. The problem was the zipFiles function.
Here's the working one:

<------------ [CODE] ------------>
public function zipFiles($zipList){

foreach($zipList as $toZip){
if(is_dir($toZip)){
if(substr($toZip, -1) == '/'){
$dir = substr($toZip, 0, (strlen($toZip)-2) );
}else{
$dir = $toZip;
}
$dir = end(explode('/', $dir));
$this->zipDir($toZip, $dir);
}else{
$fileContents = file_get_contents($toZip);
$this -> addFile($fileContents, $toZip);
}
}

}

<------------ [/CODE] ------------>