View Full Version : Zipping and Taring in php5 on Apache2

08-28-2008, 03:25 PM

I'm trying to use the Pear libraries Zip.php and Tar.php, but they are written for php4, currently unsupported, and won't run under php5. I have been chasing the problem, and do know now that the files to be zipped are properly copied to a temporary directory, and that a .zip or .tar.gz is generated with essentially empty contents. the .zip is totally empty, but the .tar.gz does have some hex string(s) added to it of short length, and I think this could be the compression header.

So, my question is how do others do this? Has anyone fixed these scripts? My files are very large, and if the user selects several, it could easily exceed the server memory limit. This problem is apparent with other existing solutions, so I should avoid them, since the above scripts compress one at a time and append them to the growing compressed file.

I guess my other solution is to get php4 installed as a cgi resource on the server, but the IT guy really thinks the code should be fixed. I have the Tar.php and Zip.php source code attached it if it is worthwhile, but I fear that this is a problem that would be very difficult for me to solve.

08-28-2008, 03:42 PM

08-28-2008, 06:24 PM
Bad Link. Did search, but didn't find anything related to programming.....they don't provide scripts.

08-28-2008, 06:45 PM
Whoops. Pasted it wrong. Try it again.

09-08-2008, 03:15 PM
Back on this thread....... I have tried using the zlib functions, and the php zip functions, but the best seems to use the shell to the system zip functions. However, I still run out of memory sometimes, or all the time if the user isn't careful. Most of the files are 3.6MB to 485.2MB, so if a user selects more than a couple of files, everything hangs. There must be some way to manage this by zipping in chunks, maybe. I need some advice from someone who has solved this problem. Right now, I'm doing this (the working part of the function):
chdir($dir); // Change current working directory, so that the zip file gets created in the temp dir
$zfiles = directoryToArray("./", true); // Get contents of the temp directory before we create our zip file, recursive
switch($type) // Add other compression methods?
case 'zip': $name .= '.zip'; $z = 1; break;
case 'tar': $name .= '.tar.gz'; $z = 2; break;
default: $name .= '.zip'; $z = 1;
foreach($zfiles as $zfile => $timestamp){
list($zfpath,$zfname,$ftotal) = stripPath('./', $zfile);
if($z == 1) {
$output = `zip -r $name $zftotal $zfpath`; // add something to show error number
} else {
if(file_exists($name)) {
$output = `tar -rvz $name $zfpath`;
} else {
$output = `tar -cvz $name $zfpath`;
$testresult = ob_get_contents(); // debug line:
ob_end_clean(); // debug line:
fwrite($fh, $testresult); // debug line:
fclose($fh); // debug line:
return $dir . $name;
The name of the temp archive is passed to this function and pushed to the web user:
function f_download($file_name) {
global $myFile, $user;
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } // required for IE
// $file_name = str_replace(array("/",".."),"",$file_name);
case 'pdf': $mime = 'application/pdf'; break;
case 'hdf': $mime = 'application/x-hdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpg': $mime = 'image/jpg'; break;
case 'png': $mime = 'image/png'; break;
case 'cdf': $mime = 'application/x-netcdf'; break;
case 'hdf': $mime = 'application/x-hdf'; break;
default: $mime = 'application/force-download';
ob_clean_all(); // clean output buffer before the file transfer set-up
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
readfile($file_name); // push it out
$fh = fopen($myFile,'a') or die('cannot open file: ' . $myFile); // open download log file
$stringData = date("Y/m/d H:i:s")." ".$user['user_name']." ".$_SERVER['REMOTE_ADDR']." downloaded ".$file_name."\n";
fwrite($fh, $stringData); // write download transaction
exit(); // at sometime, we need to clean-up the tmp directory
Can anyone help?