на вот держи:
<?php
function TarAddHeader( $f, $phisfn, $archfn ) {
$info = stat( $phisfn );
$ouid = sprintf( "%6s ", decoct( $info[ 4 ] ) );
$ogid = sprintf( "%6s ", decoct( $info[ 5 ] ) );
$omode = sprintf( "%6s ", decoct( fileperms( $phisfn ) ) );
$omtime = sprintf( "%11s", decoct( filemtime( $phisfn ) ) );
if ( @is_dir( $phisfn ) ) {
$type = "5";
$osize = sprintf( "%11s ", decoct( 0 ) );
} else {
$type = '';
$osize = sprintf( "%11s ", decoct( filesize( $phisfn ) ) );
clearstatcache();
}
$dmajor = '';
$dminor = '';
$gname = '';
$linkname = '';
$magic = '';
$prefix = '';
$uname = '';
$version = '';
$chunkbeforeCS = pack( "a100a8a8a8a12A12", $archfn, $omode, $ouid, $ogid, $osize, $omtime );
$chunkafterCS = pack( "a1a100a6a2a32a32a8a8a155a12", $type, $linkname, $magic, $version, $uname, $gname, $dmajor, $dminor ,$prefix, '' );
$checksum = 0;
for ( $i = 0; $i < 148; $i++ ) $checksum += ord( substr( $chunkbeforeCS, $i, 1 ) );
for ( $i = 148; $i < 156; $i++ ) $checksum += ord(' ');
for ( $i = 156, $j = 0; $i < 512; $i++, $j++ ) $checksum += ord( substr( $chunkafterCS, $j, 1 ) );
fwrite( $f, $chunkbeforeCS, 148 );
$checksum = sprintf( "%6s ", decoct( $checksum ) );
$bdchecksum = pack( "a8", $checksum );
fwrite( $f, $bdchecksum, 8 );
fwrite( $f, $chunkafterCS, 356 );
return true;
}
function TarWriteContents( $f, $phisfn ) {
if ( @is_dir( $phisfn ) ) {
return;
} else {
$size = filesize( $phisfn );
$padding = $size % 512 ? 512 - $size % 512 : 0;
$f2 = fopen( $phisfn, "rb" );
while ( !feof( $f2 ) ) fwrite( $f, fread( $f2, 1024 * 1024 ) );
fclose( $f2 );
$pstr = sprintf( "a%d", $padding );
fwrite( $f, pack( $pstr, '' ) );
}
}
function TarAddFooter( $f ) {
fwrite( $f, pack( 'a1024', '' ) );
}
function getRecursiveDir( $dir, $archdir = '' ) {
$out = array();
if ( file_exists( $dir ) ) {
if ( is_dir( $dir ) ) {
$getcontent_dir_type = dir( $dir );
while ( false !== ( $entry_type = $getcontent_dir_type->read() ) ) {
if ( ( $entry_type !== '.' ) && ( $entry_type !== '..' ) ) {
$out[] = $archdir.$entry_type;
if ( filetype( $dir."/".$entry_type ) == 'dir')
{
$rec = getRecursiveDir( $dir."/".$entry_type, $archdir.$entry_type.'/' );
$out = array_merge( $out, $rec );
}
}
}
$getcontent_dir_type->close();
}
}
return $out;
}
// тут указываем какую папку хотим засунуть в tar файл
$directory_to_pack = dirname( __FILE__ ).'/forpackdir';
$tar = fopen("mytar.tar", "w");
$files = getRecursiveDir( $directory_to_pack );
foreach( $files as $record ) {
TarAddHeader( $tar, $directory_to_pack.'/'.$record, $record );
TarWriteContents( $tar, $directory_to_pack.'/'.$record );
}
TarAddFooter( $tar );
fclose( $tar );
// ну а потом можно tar файл уже и упаковывать zlib'ом
?>