PHP使用zLib压缩多个文件

时间:2011-03-02 01:50:06

标签: php zlib multiple-files

$files = array("images/1.jpg", "images/2.jpg", "images/3.jpg"); 
foreach($files as $file){ 
    $temp = null; 
    $fp_in = fopen($file,'rb'); 
    while(!feof($fp_in)){ 
        $temp .= fread($fp_in,1024); 
    } 
    $output[$file] = $temp; 
    fclose($fp_in); 
} 

$output = implode('"',$output);

$zp = gzopen( 'sequences/backup.gz', "w9" );
gzwrite( $zp, $output );
gzclose( $zp );

上面的代码有效,但只有一个文件被添加到存档中。使用zLib将多个文件添加到存档的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

require 'Tar.php';
$tar_object = new Archive_Tar("tarname.tar");
$tar_object->setErrorHandling(PEAR_ERROR_PRINT);  // Optional error handling
$v_list = array("images/1.jpg", "images/2.jpg", "images/3.jpg"); 
$tar_object->createModify($v_list, "install");
  function compress( $srcFileName, $dstFileName ){
   // getting file content
   $fp = fopen( $srcFileName, "r" );
   $data = fread ( $fp, filesize( $srcFileName ) );
   fclose( $fp );
   // writing compressed file
   $zp = gzopen( $dstFileName, "w9" );
   gzwrite( $zp, $data );
   gzclose( $zp );
   echo 'success';
}
compress("tarname.tar","tarname.tar.gz");
unlink('tarname.tar');

这是更新的代码,在蒂姆的帮助下,谢谢!