5
我想使用PHP壓縮(即時)tar.gz
格式的目錄。我知道可以使用exec
或使用this code
(使用ZIP格式)來完成,但我的主機不支持exec,並且沒有安裝PHP的zip擴展名。使用PHP實時壓縮tar.gz格式的目錄使用PHP
搜索互聯網後,我碰到this PHP code
:
<?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.txt
和test2.txt
)。
但是我怎麼用這個來下載一個目錄(和子目錄)呢?
我的目錄是這樣的:
home/
file1.html
file2.html
Another_Dir/
file8.html
Sub_Dir/
file19.html
我正在使用PHP框架來測試,所以'最後一行'返回'也許不必要.. – 2013-05-12 18:53:08
謝謝!但是當有一個子目錄('home/Another_Dir/Sub_Dir /')時,他將該文件夾移動到'backup.tar.gz'文件中的('home/Sub_Dir /'),並且他不保持正常的目錄格式'家用/ Another_Dir/Sub_Dir /')。這可以修復嗎? – user1480019 2013-05-12 19:48:32
@GerritHoekstra我編輯了代碼,以便現在應該修復。 – 2013-05-12 22:09:57