使用PHP实现动态压缩一个目录为tar.gz格式的文件。

5
我想使用PHP压缩(即时压缩)一个目录,格式为tar.gz。我知道可以使用exec或使用这段代码(使用ZIP格式)来完成,但我的主机不支持exec并且未安装PHP的zip扩展程序。

在搜索互联网后,我找到了这个PHP代码

<?php
// Computes the unsigned Checksum of a file’s header
// to try to ensure valid file
// PRIVATE ACCESS FUNCTION
function __computeUnsignedChecksum($bytestring) {
  for($i=0; $i<512; $i++)
    $unsigned_chksum += ord($bytestring[$i]);
  for($i=0; $i<8; $i++)
    $unsigned_chksum -= ord($bytestring[148 + $i]);
  $unsigned_chksum += ord(" ") * 8;

  return $unsigned_chksum;
}

// Generates a TAR file from the processed data
// PRIVATE ACCESS FUNCTION
function tarSection($Name, $Data, $information=NULL) {
  // Generate the TAR header for this file

  $header .= str_pad($Name,100,chr(0));
  $header .= str_pad("777",7,"0",STR_PAD_LEFT) . chr(0);
  $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  $header .= str_pad(decoct(strlen($Data)),11,"0",STR_PAD_LEFT) . chr(0);
  $header .= str_pad(decoct(time(0)),11,"0",STR_PAD_LEFT) . chr(0);
  $header .= str_repeat(" ",8);
  $header .= "0";
  $header .= str_repeat(chr(0),100);
  $header .= str_pad("ustar",6,chr(32));
  $header .= chr(32) . chr(0);
  $header .= str_pad($information["user_name"],32,chr(0));
  $header .= str_pad($information["group_name"],32,chr(0));
  $header .= str_repeat(chr(0),8);
  $header .= str_repeat(chr(0),8);
  $header .= str_repeat(chr(0),155);
  $header .= str_repeat(chr(0),12);

  // Compute header checksum
  $checksum = str_pad(decoct(__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
  for($i=0; $i<6; $i++) {
    $header[(148 + $i)] = substr($checksum,$i,1);
  }
  $header[154] = chr(0);
  $header[155] = chr(32);

  // Pad file contents to byte count divisible by 512
  $file_contents = str_pad($Data,(ceil(strlen($Data) / 512) * 512),chr(0));

  // Add new tar formatted data to tar file contents
  $tar_file = $header . $file_contents;

  return $tar_file;
}

function targz($Name, $Data) {
  return gzencode(tarSection($Name,$Data),9);
}

header("Content-Disposition: attachment; filename=backup.tar.gz");
header("Content-type: application/x-gzip");

$getfile = file_get_contents("test.txt");

echo targz('test.txt', $getfile);
?>

这可以用于多个文件,方法如下:
header("Content-Disposition: attachment; filename=backup.tar.gz");
header("Content-type: application/x-gzip");

$getfile = file_get_contents("test.txt");

echo targz('test.txt', $getfile);

$getfile2 = file_get_contents("test2.txt");

echo targz('test2.txt', $getfile2);

backup.tar.gz中现在有2个文件(test.txttest2.txt)。

但是我该如何使用它来下载一个目录(和子目录)?

我的目录大致如下:

home/
    file1.html
    file2.html
Another_Dir/
    file8.html
    Sub_Dir/
        file19.html
1个回答

4
你需要列出要压缩的目录中的所有文件和目录。这里有一个递归目录列表函数(此处)。我认为将你的代码与他的代码结合起来就能完成工作,类似于这样:
header("Content-Disposition: attachment; filename=backup.tar.gz");
header("Content-type: application/x-gzip");

// Your other tar.gz functions...

function compress( $path = '.', $level = 0 ){ 
    $ignore = array( 'cgi-bin', '.', '..' ); 
    $dh = @opendir( $path );

    while( false !== ( $file = readdir( $dh ) ) ){                                   
        if( !in_array( $file, $ignore ) ){
            if( is_dir( "$path/$file" ) ){
                // Go to the subdirs to compress files inside the subdirs
                compress( "$path/$file", ($level+1) );                                           
            } else {
                $getfile = file_get_contents($path. '/' .$file);
                // get the last dir in the $path
                $dirs = array_filter( explode('/', $path) );
                $dir = array_pop($dirs);

                // if we're not in a sub dir just compress the file in root otherwise add it on the subdir...
                if ($level < 1) {
                    echo targz($file, $getfile);
                } else {
                    // add all top level subdirs when $level is > 2
                    for ($i = 0; $i < $level - 1; $i++) {
                        $temp = array_pop($dirs);
                        $dir = $temp.'/'.$dir;
                    }
                    echo targz($dir. '/' .$file, $getfile);
                }
            }                                        
        }                                    
    } 

    closedir( $dh ); 
}

$dir    = 'dir/to/compress';
if (is_file($dir)) {
    // If it's a file path just compress it & push it to output
    $getfile = file_get_contents($dir);
    echo targz(basename($dir), $getfile);
} elseif(is_dir($dir)) {
    return compress($dir);
}

对我来说它工作得很好。这是我的第一次贡献,希望它有所帮助。


我正在使用一个PHP框架进行测试,因此最后一行的return可能是不必要的。 - Hamed Nemati
1
@GerritHoekstra 我已经编辑了代码,现在应该已经修复了。 - Hamed Nemati
1
@GerritHoekstra 刚刚编辑了代码的最后一部分以处理那个问题。 - Hamed Nemati
还有一个问题,我该如何使用它将其放入服务器上的tar.gz文件中(而不是即时处理)?对于1个文件,它可以工作: file_put_contents('home / backup.tar.gz',targz(basename($dir),$getfile));,但当我尝试这样做时:file_put_contents('home / backup.tar.gz',compress($dir)); 它无法正常工作。希望你能帮助我解决这个问题。 - user1480019
在Ubuntu上使用tar命令时出现错误:存档包含“png00007”,但期望是数字模式(mode_t)值:( - e-info128
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接