Results 1 to 5 of 5

Thread: Zipping and Taring in php5 on Apache2

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default Zipping and Taring in php5 on Apache2

    Hi,

    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.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Last edited by techietim; 08-28-2008 at 06:45 PM.

  3. #3
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Bad Link. Did search, but didn't find anything related to programming.....they don't provide scripts.

  4. #4
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Whoops. Pasted it wrong. Try it again.

  5. #5
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    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):
    Code:
            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:
    Code:
    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);  
         switch(strtolower(substr(strrchr($file_name,'.'),1)))
         {  
             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
         fclose($fh);          
         exit();                                                                    // at sometime, we need to clean-up the tmp directory
    }
    Can anyone help?
    Last edited by Strangeplant; 09-08-2008 at 03:22 PM. Reason: added the output script

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •