📄 tar.php
字号:
<?php/*+--------------------------------------------------------------------------| Invision Power Board v2.1.5| =============================================| by Matthew Mecham| (c) 2001 - 2005 Invision Power Services, Inc.| | =============================================| Web: | Time: Wed, 01 Mar 2006 19:11:29 GMT| Release: | Licence Info: +---------------------------------------------------------------------------| > $Date: 2005-12-01 16:45:12 +0000 (Thu, 01 Dec 2005) $| > $Revision: 88 $| > $Author: bfarber $+---------------------------------------------------------------------------|| > Tar creation and extraction module| > Module written by Matt Mecham| > Usage style based on the C and Perl modules| > Will only work with PHP 4+| | > Date started: 15th Feb 2002|| > Module Version Number: 1.0.0| > Module Author: Matthew Mecham+--------------------------------------------------------------------------|| QUOTE OF THE MODULE:| If you can't find a program the does what you want it to do, write your| own.|+--------------------------------------------------------------------------*//*************************************************************|| EXTRACTION USAGE:|| $tar = new tar();| $tar->new_tar("/foo/bar", "myTar.tar");| $files = $tar->list_files();| $tar->extract_files( "/extract/to/here/dir" );|| CREATION USAGE:|| $tar = new tar();| $tar->new_tar("/foo/bar" , "myNewTar.tar");| $tar->current_dir("/foo" ); //Optional - tells the script which dir we are in| to syncronise file creation from the tarball| $tar->add_files( $file_names_with_path_array );| (or $tar->add_directory( "/foo/bar/myDir" ); to archive a complete dir)| $tar->write_tar();|*************************************************************/class tar { var $tar_header_length = '512'; var $tar_unpack_header = 'a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typeflag/a100linkname/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155/prefix'; var $tar_pack_header = 'A100 A8 A8 A8 A12 A12 A8 A1 A100 A6 A2 A32 A32 A8 A8 A155'; var $current_dir = ""; var $unpack_dir = ""; var $pack_dir = ""; var $error = ""; var $work_dir = array(); var $tar_in_mem = array(); var $tar_filename = ""; var $filehandle = ""; var $warnings = array(); var $attributes = array(); var $tarfile_name = ""; var $tarfile_path = ""; var $tarfile_path_name = ""; var $workfiles = array(); //----------------------------------------- // CONSTRUCTOR: Attempt to guess the current working dir. //----------------------------------------- function tar_init() { $this->ipsclass->load_language('lang_tar'); if ($this_dir = getcwd()) { $this->current_dir = $this_dir; } else if (isset($_SERVER['DOCUMENT_ROOT'])) { $this->current_dir = $_SERVER['DOCUMENT_ROOT']; } else { $this->current_dir = './'; } // Set some attributes, these can be overriden later $this->attributes = array( 'over_write_existing' => 0, 'over_write_newer' => 0, 'remove_tar_file' => 0, 'remove_original_files' => 0, ); } //----------------------------------------- // Set the tarname. If we are extracting a tarball, then it must be the // path to the tarball, and it's name (eg: $tar->new_tar("/foo/bar" ,'myTar.tar') // or if we are creating a tar, then it must be the path and name of the tar file // to create. //----------------------------------------- function new_tar($tarpath, $tarname) { $this->tarfile_name = $tarname; $this->tarfile_path = $tarpath; // Make sure there isn't a trailing slash on the path $this->tarfile_path = preg_replace( "#[/\\\]$#" , "" , $this->tarfile_path ); $this->tarfile_path_name = $this->tarfile_path .'/'. $this->tarfile_name; } //----------------------------------------- // Easy way to overwrite defaults //----------------------------------------- function over_write_existing() { $this->attributes['over_write_existing'] = 1; } function over_write_newer() { $this->attributes['over_write_newer'] = 1; } function remove_tar_file() { $this->attributes['remove_tar_file'] = 1; } function remove_original_files() { $this->attributes['remove_original_files'] = 1; } //----------------------------------------- // User assigns the root directory for the tar ball creation/extraction //----------------------------------------- function current_dir($dir = "") { $this->current_dir = $dir; } //----------------------------------------- // list files: returns an array with all the filenames in the tar file //----------------------------------------- function list_files($advanced="") { // $advanced == "" - return name only // $advanced == 1 - return name, size, mtime, mode $data = $this->read_tar(); $final = array(); foreach($data as $d) { if ($advanced == 1) { $final[] = array ( 'name' => $d['name'], 'size' => $d['size'], 'mtime' => $d['mtime'], 'mode' => substr(decoct( $d['mode'] ), -4), ); } else { $final[] = $d['name']; } } return $final; } //----------------------------------------- // Add a directory to the tar files. // $tar->add_directory( str(TO DIRECTORY) ) // Can be used in the following methods. // $tar->add_directory( "/foo/bar" ); // $tar->write_tar( "/foo/bar" ); //----------------------------------------- function add_directory( $dir ) { $this->error = ""; // Make sure the $to_dir is pointing to a valid dir, or we error // and return if (! is_dir($dir) ) { $this->error = sprintf( $this->ipsclass->lang['error_tar1'], $to_dir ); return FALSE; } $cur_dir = getcwd(); chdir($dir); $this->get_dir_contents("./"); $this->add_files($this->workfiles, $dir); chdir($cur_dir); } //----------------------------------------- function get_dir_contents( $dir ) { $dir = preg_replace( "#/$#", "", $dir ); if ( file_exists($dir) ) { if ( is_dir($dir) ) { $handle = opendir($dir); while (($filename = readdir($handle)) !== false) { if (($filename != ".") && ($filename != "..")) { if (is_dir($dir."/".$filename)) { $this->get_dir_contents($dir."/".$filename); } else { $this->workfiles[] = $dir."/".$filename; } } } closedir($handle); } else { $this->error = sprintf( $this->ipsclass->lang['error_tar2'], $dir ); return FALSE; } } else { $this->error = sprintf( $this->ipsclass->lang['error_tar3'], $dir ); return; } } //----------------------------------------- // Extract the tarball // $tar->extract_files( str(TO DIRECTORY), [ array( FILENAMES ) ] ) // Can be used in the following methods. // $tar->extract( "/foo/bar" , $files ); // This will seek out the files in the user array and extract them // $tar->extract( "/foo/bar" ); // Will extract the complete tar file into the user specified directory //----------------------------------------- function extract_files( $to_dir, $files="" ) { $this->error = ""; // Make sure the $to_dir is pointing to a valid dir, or we error // and return if (! is_dir($to_dir) ) { $this->error = sprintf( $this->ipsclass->lang['error_tar1'], $to_dir ); return; } //----------------------------------------- // change into the directory chosen by the user. //----------------------------------------- chdir($to_dir); $cur_dir = getcwd(); $to_dir_slash = $to_dir . "/"; //----------------------------------------- // Get the file info from the tar //----------------------------------------- $in_files = $this->read_tar(); if ($this->error != "") { return; } foreach ($in_files as $k => $file) { //----------------------------------------- // Are we choosing which files to extract? //----------------------------------------- if (is_array($files)) { if (! in_array($file['name'], $files) ) { continue; } } chdir($cur_dir); //----------------------------------------- // GNU TAR format dictates that all paths *must* be in the *nix // format - if this is not the case, blame the tar vendor, not me! //----------------------------------------- if ( preg_match("#/#", $file['name']) ) { $path_info = explode( "/" , $file['name'] ); $file_name = array_pop($path_info); } else { $path_info = array(); $file_name = $file['name']; } //----------------------------------------- // If we have a path, then we must build the directory tree //----------------------------------------- if (count($path_info) > 0) { foreach($path_info as $dir_component) { if ($dir_component == "") { continue; } if ( (file_exists($dir_component)) && (! is_dir($dir_component)) ) { $this->warnings[] = sprintf( $this->ipsclass->lang['error_tar4'], $dir_component ); continue; } if (! is_dir($dir_component)) { mkdir( $dir_component, 0777); chmod( $dir_component, 0777); } if (! @chdir($dir_component)) { $this->warnings[] = sprintf( $this->ipsclass->lang['error_tar5'], $dir_component ); } } } //----------------------------------------- // check the typeflags, and work accordingly //----------------------------------------- if (($file['typeflag'] == 0) or (!$file['typeflag']) or ($file['typeflag'] == "")) { if ( $FH = fopen($file_name, "wb") ) { fputs( $FH, $file['data'], strlen($file['data']) ); fclose($FH); } else { $this->warnings[] = sprintf( $this->ipsclass->lang['error_tar6'], $file_name ); } } else if ($file['typeflag'] == 5) { if ( (file_exists($file_name)) && (! is_dir($file_name)) ) { $this->warnings[] = sprintf( $this->ipsclass->lang['error_tar7'], $file_name ); continue; } if (! is_dir($file_name)) { @mkdir( $file_name, 0777); } } else if ($file['typeflag'] == 6) { $this->warnings[] = $this->ipsclass->lang['error_tar8']; continue; } else if ($file['typeflag'] == 1) { $this->warnings[] = $this->ipsclass->lang['error_tar9']; } else if ($file['typeflag'] == 4) { $this->warnings[] = $this->ipsclass->lang['error_tar11']; } else if ($file['typeflag'] == 3) { $this->warnings[] = $this->ipsclass->lang['error_tar11']; } else { $this->warnings[] = $this->ipsclass->lang['error_tar12']; } if (! @chmod( $file_name, $file['mode'] ) ) { $this->warnings[] = sprintf( $this->ipsclass->lang['error_tar13'], $mode, $file_name ); } @touch( $file_name, $file['mtime'] ); } // Return to the "real" directory the scripts are in @chdir($this->current_dir);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -