/**
* Generates a zip.
*
* @param string $dir
* @return void
* @author Thibaud Rohmer
*/
public static function Zip($dir){

/// Check that user is allowed to acces this content
if( !Judge::view($dir)){
return;
}	


                // Get the relative path of the files
$delimPosition = strrpos($dir, '/');
if (strlen($dir) == $delimPosition) {
echo "Error: Directory has a slash at the end";
return;
}

// Create list with all filenames
$items = Menu::list_files($dir,true);
$itemsString = '';

foreach($items as $item){
if(Judge::view($item)){
                                // Use only the relative path of the filename
$item = str_replace('//', '/', $item);
$itemsString.=" '".substr($item,$delimPosition+1)."'";
}
}

// Close and send to user
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=\"".htmlentities(basename($dir), ENT_QUOTES ,'UTF-8').".zip\"");

                // Store the current working directory and change to the albums directory
$cwd = getcwd();
chdir(substr($dir,0,$delimPosition));

// ZIP-on-the-fly method copied from http://stackoverflow.com/questions/4357073
//
// use popen to execute a unix command pipeline
// and grab the stdout as a php stream
$fp = popen('zip -n .jpg:.JPG:.jpeg:.JPEG -0 - ' . $itemsString, 'r');

if(!is_resource($fp)) {
return false;
}

// pick a bufsize that makes you happy (8192 has been suggested).
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
$buff = fread($fp, $bufsize);
                        echo $buff;
                        /// flush();
                }
                pclose($fp);
                
                // Chang to the previous working directory
                chdir($cwd);
}