Tar/zlib algorithm implementation module

Source: Internet
Author: User
? /* Copyright (c) 2001 xinshiallrightsreservedhttp: // xinshi. orgmailto: y10k@963.netoicq: 6795798 */Classtarlib {var $ tarname; var $ filehand0; functioncheckcompress () {global /*
Copyright (c) 2001 xinshi allrights reserved

Http://xinshi.org
Mailto: y10k@963.net
Oicq: 6795798
*/
Class tarlib {

Var $ tarname = ';
Var $ filehand = 0;

Function checkcompress (){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

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 ';
}
}

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;
}
}

Function checksum ($ binary_data_first, $ binary_data_last = '){
If ($ binary_data_last = '){
$ Binary_data_last = $ binary_data_first;
}
$ Checksum = 0;
For ($ I = 0; I 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;
}
}

Class tar extends tarlib {

Var $ filelist = array ();

Function tar ($ tarname, $ filelist ){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ This-> tarname = $ tarname;
$ This-> checkcompress ();
$ This-> filelist = is_array ($ filelist )? $ Filelist: explode ('', $ filelist );

$ This-> create ();
}

Function create (){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ This-> filehand =$ _ dofunc_open ($ this-> tarname, 'WB ');

$ This-> parse ($ this-> filelist );
$ This-> footer ();

$ _ Dofunc_close ($ this-> filehand );
}

Function parse ($ filelist ){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ 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 );
}
}

Function parseFile ($ filename ){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ V_info = stat ($ filename );
$ V_uid = sprintf ('% 6 s', DecOct ($ v_info [4]);
$ V_gid = sprintf ('% 6 s', DecOct ($ v_info [5]);
$ V_perms = sprintf ('% 6 s', DecOct (fileperms ($ filename )));
Clearstatcache ();
$ V_size = filesize ($ filename );
$ V_size = sprintf ('% 11 s', DecOct ($ v_size ));
$ V_mtime = sprintf ('% 11s', DecOct (filemtime ($ filename )));

$ 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 );

$ V_checksum = $ this-> checksum ($ v_binary_data_first, $ v_binary_data_last );

$ V_checksum = sprintf ('% 6 s', 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 );

$ Fp = fopen ($ filename, 'RB ');
While ($ buffer = fread ($ fp, 512) <> '){
$ Binary_buffer = pack ('a512', $ buffer );
$ _ Dofunc_write ($ this-> filehand, $ binary_buffer );
}
}

Function footer (){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ V_binary_data = pack ('a512 ',');
$ _ Dofunc_write ($ this-> filehand, $ v_binary_data );
}
}


Class tarExtract extends tarlib {

Var $ extractDir = './extract ';

Function tarExtract ($ tarname, $ extractDir = './extract '){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ This-> tarname = $ tarname;
$ This-> extractDir = $ extractDir;
$ This-> checkcompress ();

If (! Is_dir ($ extractDir )){
$ This-> mkdir ($ extractDir );
}
$ This-> extract ();
}

Function extract (){
Global $ _ dofunc_open, $ _ dofunc_close, $ _ dofunc_read, $ _ dofunc_write;

$ 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 );

$ 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 );
}

Function parseHeader ($ header ){

$ Checksum = $ this-> checksum ($ header );
$ Data = unpack ('a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/container/name ', $ header );

$ File ['checksum'] = Dec (trim ($ data ['checksum']);
/*
If ($ file ['checksum'] <> $ checksum ){
Return false;
}
*/
$ File ['name'] = trim ($ data ['filename']);
$ File ['mode'] = Dec (trim ($ data ['mode']);
$ File ['uid'] = OctDec (trim ($ data ['uid']);
$ File ['gid'] = Dec (trim ($ data ['gid']);
$ File ['size'] = Dec (trim ($ data ['size']);
$ File ['mtime'] = Dec (trim ($ data ['mtime']);
$ File ['type'] = $ data ['typeflag'];

Return $ file;
}
}


/*
Examples:
1、decompress xxx.tar to the mydemo Directory (if zlib is used for compression
The xxx.tar.gz file is automatically decompressed by the zlib module.
-------------------------
Require_once 'tarlib. inc. php ';

New tarExtract('xxx.tar ', 'mydemo ');
-------------------------

2. compress the file into a files.tar.gz file.
-------------------------
Require_once 'tarlib. inc. php ';

New tar('files.tar.gz ', 'file ');
-------------------------
*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.