tar/zlib算法实现模块
时间:2007-02-17 来源:PHP爱好者
<?
/*
copyright (c) 2001 xinshi allrights reserved
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
http://xinshi.org
mailto:[email protected]
oicq:6795798
*/
Class tarlib {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
var $tarname = '';
var $filehand = 0;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function checkcompress() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
if((substr($this->tarname, -7)=='.tar.gz') || (substr($this->tarname, -4)=='.tgz')) {
$_dofunc_open = 'gzopen';
$_dofunc_close = 'gzclose';
$_dofunc_read = 'gzread';
$_dofunc_write = 'gzwrite';
} else {
$_dofunc_open = 'fopen';
$_dofunc_close = 'fclose';
$_dofunc_read = 'fread';
$_dofunc_write = 'fwrite';
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function mkdir($dir) {
$dirlist = explode('/', $dir);
$depth = count($dirlist)-1;
$dir = $dirlist[0];
for($i = 0; $i<$depth; $i++) {
if(!is_dir($dir)) {
mkdir($dir, 0777);
}
$dir.= '/'.$dirlist[$i+1];
$last = $off;
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function checksum($binary_data_first, $binary_data_last = '') {
if($binary_data_last=='') {
$binary_data_last = $binary_data_first;
}
$checksum = 0;
for ($i=0; $i<148; $i++) {
$checksum += ord(substr($binary_data_first, $i, 1));
}
for ($i=148; $i<156; $i++) {
$checksum += ord(' ');
}
for ($i=156, $j=0; $i<512; $i++, $j++) {
$checksum += ord(substr($binary_data_last, $j, 1));
}
return $checksum;
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
Class tar extends tarlib {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
var $filelist = array();
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function tar($tarname, $filelist) {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->tarname = $tarname;
$this->checkcompress();
$this->filelist = is_array($filelist) ? $filelist : explode(' ', $filelist);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->create();
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function create() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->filehand = $_dofunc_open($this->tarname, 'wb');
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->parse($this->filelist);
$this->footer();
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$_dofunc_close($this->filehand);
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function parse($filelist) {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$files = count($filelist);
for($i = 0; $i <$files; $i++) {
$filename = $filelist[$i];
if(is_dir($filename)) {
$dirh = opendir($filename);
readdir($dirh); // '.'
readdir($dirh); // '..'
while($nextfile = readdir($dirh)) {
$temp_filelist[] = ($filename != '.') ? $filename.'/'.$nextfile : $nextfile;
}
$this->parse($temp_filelist);
closedir($dirh);
unset($dirh);
unset($temp_filelist);
unset($nextfile);
continue;
}
$this->parseFile($filename);
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function parseFile($filename) {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_info = stat($filename);
$v_uid = sprintf('%6s ', DecOct($v_info[4]));
$v_gid = sprintf('%6s ', DecOct($v_info[5]));
$v_perms = sprintf('%6s ', DecOct(fileperms($filename)));
clearstatcache();
$v_size = filesize($filename);
$v_size = sprintf('%11s ', DecOct($v_size));
$v_mtime = sprintf('%11s', DecOct(filemtime($filename)));
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_binary_data_first = pack('a100a8a8a8a12A12', $filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
$v_binary_data_last = pack('a1a100a6a2a32a32a8a8a155a12', '', '', '', '', '', '', '', '', '', '');
$_dofunc_write($this->filehand, $v_binary_data_first, 148);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_checksum = $this->checksum($v_binary_data_first, $v_binary_data_last);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_checksum = sprintf('%6s ', DecOct($v_checksum));
$v_binary_data = pack('a8', $v_checksum);
$_dofunc_write($this->filehand, $v_binary_data, 8);
$_dofunc_write($this->filehand, $v_binary_data_last, 356);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$fp = fopen($filename, 'rb');
while(($buffer = fread($fp, 512)) <> '') {
$binary_buffer = pack('a512', $buffer);
$_dofunc_write($this->filehand, $binary_buffer);
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function footer() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_binary_data = pack('a512', '');
$_dofunc_write($this->filehand, $v_binary_data);
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
Class tarExtract extends tarlib {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
var $extractDir = './extract';
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function tarExtract($tarname, $extractDir = './extract') {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->tarname = $tarname;
$this->extractDir = $extractDir;
$this->checkcompress();
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
if(!is_dir($extractDir)) {
$this->mkdir($extractDir);
}
$this->extract();
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function extract() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->filehand = $_dofunc_open($this->tarname, 'rb');
while(($binary_buffer = fread($this->filehand, 512)) <> '') {
$file = $this->parseHeader($binary_buffer);
if(!$file['name']) continue;
$file['name'] = $this->extractDir.'/'.$file['name'];
$readtimes = floor($file['size']/512);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->mkdir($file['name']);
$fp = fopen($file['name'], 'wb');
for($i = 0; $i<$readtimes; $i++) {
fwrite($fp, $_dofunc_read($this->filehand, 512));
}
if(($lastsize = $file['size']%512)) {
fwrite($fp, $_dofunc_read($this->filehand, 512), $lastsize);
}
fclose($fp);
}
$_dofunc_close($this->filehand);
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function parseHeader($header) {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$checksum = $this->checksum($header);
$data = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $header);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$file['checksum'] = OctDec(trim($data['checksum']));
/*
if($file['checksum']<>$checksum) {
return false;
}
*/
$file['name'] = trim($data['filename']);
$file['mode'] = OctDec(trim($data['mode']));
$file['uid'] = OctDec(trim($data['uid']));
$file['gid'] = OctDec(trim($data['gid']));
$file['size'] = OctDec(trim($data['size']));
$file['mtime'] = OctDec(trim($data['mtime']));
$file['type'] = $data['typeflag'];
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
return $file;
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
/*
examples:
1、将xxx.tar解压到mydemo目录(如果是用zlib压缩
过的xxx.tar.gz文件,会自动调用zlib模块解压
-------------------------
require_once 'tarlib.inc.php';
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
new tarExtract('xxx.tar', 'mydemo');
-------------------------
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
2、将file目录压缩成为files.tar.gz文件
-------------------------
require_once 'tarlib.inc.php';
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
new tar('files.tar.gz', 'file');
-------------------------
*/
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
/*
copyright (c) 2001 xinshi allrights reserved
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
http://xinshi.org
mailto:[email protected]
oicq:6795798
*/
Class tarlib {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
var $tarname = '';
var $filehand = 0;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function checkcompress() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
if((substr($this->tarname, -7)=='.tar.gz') || (substr($this->tarname, -4)=='.tgz')) {
$_dofunc_open = 'gzopen';
$_dofunc_close = 'gzclose';
$_dofunc_read = 'gzread';
$_dofunc_write = 'gzwrite';
} else {
$_dofunc_open = 'fopen';
$_dofunc_close = 'fclose';
$_dofunc_read = 'fread';
$_dofunc_write = 'fwrite';
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function mkdir($dir) {
$dirlist = explode('/', $dir);
$depth = count($dirlist)-1;
$dir = $dirlist[0];
for($i = 0; $i<$depth; $i++) {
if(!is_dir($dir)) {
mkdir($dir, 0777);
}
$dir.= '/'.$dirlist[$i+1];
$last = $off;
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function checksum($binary_data_first, $binary_data_last = '') {
if($binary_data_last=='') {
$binary_data_last = $binary_data_first;
}
$checksum = 0;
for ($i=0; $i<148; $i++) {
$checksum += ord(substr($binary_data_first, $i, 1));
}
for ($i=148; $i<156; $i++) {
$checksum += ord(' ');
}
for ($i=156, $j=0; $i<512; $i++, $j++) {
$checksum += ord(substr($binary_data_last, $j, 1));
}
return $checksum;
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
Class tar extends tarlib {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
var $filelist = array();
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function tar($tarname, $filelist) {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->tarname = $tarname;
$this->checkcompress();
$this->filelist = is_array($filelist) ? $filelist : explode(' ', $filelist);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->create();
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function create() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->filehand = $_dofunc_open($this->tarname, 'wb');
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->parse($this->filelist);
$this->footer();
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$_dofunc_close($this->filehand);
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function parse($filelist) {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$files = count($filelist);
for($i = 0; $i <$files; $i++) {
$filename = $filelist[$i];
if(is_dir($filename)) {
$dirh = opendir($filename);
readdir($dirh); // '.'
readdir($dirh); // '..'
while($nextfile = readdir($dirh)) {
$temp_filelist[] = ($filename != '.') ? $filename.'/'.$nextfile : $nextfile;
}
$this->parse($temp_filelist);
closedir($dirh);
unset($dirh);
unset($temp_filelist);
unset($nextfile);
continue;
}
$this->parseFile($filename);
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function parseFile($filename) {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_info = stat($filename);
$v_uid = sprintf('%6s ', DecOct($v_info[4]));
$v_gid = sprintf('%6s ', DecOct($v_info[5]));
$v_perms = sprintf('%6s ', DecOct(fileperms($filename)));
clearstatcache();
$v_size = filesize($filename);
$v_size = sprintf('%11s ', DecOct($v_size));
$v_mtime = sprintf('%11s', DecOct(filemtime($filename)));
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_binary_data_first = pack('a100a8a8a8a12A12', $filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime);
$v_binary_data_last = pack('a1a100a6a2a32a32a8a8a155a12', '', '', '', '', '', '', '', '', '', '');
$_dofunc_write($this->filehand, $v_binary_data_first, 148);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_checksum = $this->checksum($v_binary_data_first, $v_binary_data_last);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_checksum = sprintf('%6s ', DecOct($v_checksum));
$v_binary_data = pack('a8', $v_checksum);
$_dofunc_write($this->filehand, $v_binary_data, 8);
$_dofunc_write($this->filehand, $v_binary_data_last, 356);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$fp = fopen($filename, 'rb');
while(($buffer = fread($fp, 512)) <> '') {
$binary_buffer = pack('a512', $buffer);
$_dofunc_write($this->filehand, $binary_buffer);
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function footer() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$v_binary_data = pack('a512', '');
$_dofunc_write($this->filehand, $v_binary_data);
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
Class tarExtract extends tarlib {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
var $extractDir = './extract';
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function tarExtract($tarname, $extractDir = './extract') {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->tarname = $tarname;
$this->extractDir = $extractDir;
$this->checkcompress();
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
if(!is_dir($extractDir)) {
$this->mkdir($extractDir);
}
$this->extract();
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function extract() {
global $_dofunc_open, $_dofunc_close, $_dofunc_read, $_dofunc_write;
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->filehand = $_dofunc_open($this->tarname, 'rb');
while(($binary_buffer = fread($this->filehand, 512)) <> '') {
$file = $this->parseHeader($binary_buffer);
if(!$file['name']) continue;
$file['name'] = $this->extractDir.'/'.$file['name'];
$readtimes = floor($file['size']/512);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$this->mkdir($file['name']);
$fp = fopen($file['name'], 'wb');
for($i = 0; $i<$readtimes; $i++) {
fwrite($fp, $_dofunc_read($this->filehand, 512));
}
if(($lastsize = $file['size']%512)) {
fwrite($fp, $_dofunc_read($this->filehand, 512), $lastsize);
}
fclose($fp);
}
$_dofunc_close($this->filehand);
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
function parseHeader($header) {
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$checksum = $this->checksum($header);
$data = unpack('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor', $header);
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
$file['checksum'] = OctDec(trim($data['checksum']));
/*
if($file['checksum']<>$checksum) {
return false;
}
*/
$file['name'] = trim($data['filename']);
$file['mode'] = OctDec(trim($data['mode']));
$file['uid'] = OctDec(trim($data['uid']));
$file['gid'] = OctDec(trim($data['gid']));
$file['size'] = OctDec(trim($data['size']));
$file['mtime'] = OctDec(trim($data['mtime']));
$file['type'] = $data['typeflag'];
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
return $file;
}
}
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
/*
examples:
1、将xxx.tar解压到mydemo目录(如果是用zlib压缩
过的xxx.tar.gz文件,会自动调用zlib模块解压
-------------------------
require_once 'tarlib.inc.php';
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
new tarExtract('xxx.tar', 'mydemo');
-------------------------
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
2、将file目录压缩成为files.tar.gz文件
-------------------------
require_once 'tarlib.inc.php';
c hina it power . c omvdeniAYk4edHPw0XhC6s8uJb1
new tar('files.tar.gz', 'file');
-------------------------
*/
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
相关阅读 更多 +