OSDN Git Service

9b89cfeb3c3515f8136fd28e0bedc3b2e6af14ca
[idb/iDB-Extras.git.git] / NeoSrcView / untar.php
1 <?php
2 /*
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the Revised BSD License.
5  
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9     Revised BSD License for more details.
10  
11     Copyright 2004-2011 iDB Support - http://idb.berlios.de/
12     Copyright 2004-2011 Game Maker 2k - http://gamemaker2k.org/
13     iUnTar ver. 4.7 by Kazuki Przyborowski & Josep Sanz Campderros
14  
15     $FileInfo: untar.php - Last Update: 11/14/2011 Ver 4.7 - Author: cooldude2k $
16 */
17
18 // PHP iUnTAR Version 4.7
19 // license: Revised BSD license
20 // Kazuki Przyborowski (http://ja.gamemaker2k.org/)
21 // Josep Sanz Campderros (http://saltos.net/)
22 function untar($tarfile,$outdir="./",$chmod=null,$extract=true,$lsonly=false,$findfile=null) {
23 $TarSize = filesize($tarfile);
24 $TarSizeEnd = $TarSize - 1024;
25 if($extract!==true&&$extract!==false) {
26         $extract = false; }
27 if($lsonly!==true&&$lsonly!==false) {
28         $lsonly = false; }
29 if($extract===true) { 
30         $lsonly = false; }
31 if($extract===true) {
32 if($outdir!=""&&!file_exists($outdir)) {
33         mkdir($outdir,0777); } }
34 $thandle = fopen($tarfile, "rb");
35 $i = 0;
36 if($extract===false) {
37         $FileArray = null; $i = 0; }
38 $outdir = preg_replace('{/$}', '', $outdir)."/";
39 if(isset($findfile)) {
40 $qfindfile = preg_quote($findfile,"/"); }
41 if(!isset($findfile)) {
42 $qfindfile = null; }
43 while (ftell($thandle)<$TarSizeEnd) {
44         $FileName = null;
45         $FileMode = null;
46         $OwnerID = null;
47         $GroupID = null;
48         $FileSize = null;
49         $LastEdit = null;
50         $Checksum = null;
51         $FileType = null;
52         $LinkedFile = null;
53         $FileContent = null;
54         $FileName = $outdir.trim(fread($thandle,100));
55         fseek($thandle,56,SEEK_CUR);
56         $FileType = trim(fread($thandle,1));
57         fseek($thandle,-57,SEEK_CUR);
58         if($findfile!==null&&$FileType!="L"&&!preg_match("/".$qfindfile."/",$FileName)) {
59                 fseek($thandle,8,SEEK_CUR);
60                 fseek($thandle,8,SEEK_CUR);
61                 fseek($thandle,8,SEEK_CUR);
62                 $FileSize = octdec(trim(fread($thandle,12)));
63                 fseek($thandle,12,SEEK_CUR);
64                 fseek($thandle,8,SEEK_CUR);
65                 $FileType = trim(fread($thandle,1));
66                 fseek($thandle,100,SEEK_CUR);
67                 fseek($thandle,255,SEEK_CUR); 
68                 if($FileType=="0"||$FileType=="7") {
69                         fseek($thandle,$FileSize,SEEK_CUR); } }
70         if($findfile===null||$FileType=="L"||preg_match("/".$qfindfile."/",$FileName)) {
71         $FileMode = trim(fread($thandle,8));
72         if($chmod===null) {
73                 $FileCHMOD = octdec("0".substr($FileMode,-3)); }
74         if($chmod!==null) {
75                 $FileCHMOD = $chmod; }
76                 $OwnerID = trim(fread($thandle,8));
77                 $GroupID = trim(fread($thandle,8));
78                 $FileSize = octdec(trim(fread($thandle,12)));
79                 $LastEdit = octdec(trim(fread($thandle,12)));
80                 $Checksum = octdec(trim(fread($thandle,8)));
81                 $FileType = trim(fread($thandle,1));
82                 $LinkedFile = trim(fread($thandle,100));
83                 fseek($thandle,255,SEEK_CUR); 
84                         // LongLink support added by Josep Sanz Campderros
85                         if($FileType=="L"&&$FileSize>0) {
86                                 $FileName = $outdir.trim(fread($thandle,$FileSize));
87                                 fseek($thandle,512-$FileSize,SEEK_CUR);
88                                 fseek($thandle,100,SEEK_CUR);
89                                 $FileMode = trim(fread($thandle,8));
90                                 if($chmod===null) {
91                                         $FileCHMOD = octdec("0".substr($FileMode,-3)); }
92                                 if($chmod!==null) {
93                                         $FileCHMOD = $chmod; }
94                                 $OwnerID = trim(fread($thandle,8));
95                                 $GroupID = trim(fread($thandle,8));
96                                 $FileSize = octdec(trim(fread($thandle,12)));
97                                 $LastEdit = trim(fread($thandle,12));
98                                 $Checksum = trim(fread($thandle,8));
99                                 $FileType = trim(fread($thandle,1));
100                                 $LinkedFile = trim(fread($thandle,100));
101                                 fseek($thandle,255,SEEK_CUR); } }
102                 if($findfile===null||preg_match("/".$qfindfile."/",$FileName)) {
103                 if($FileType=="0"||$FileType=="7") {
104                         if($lsonly===true) {
105                         fseek($thandle,$FileSize,SEEK_CUR); }
106                         if($lsonly===false) {
107                         // Empty files support added by Josep Sanz Campderros
108                         if($FileSize===0) {
109                         $FileContent = ""; }
110                         if($FileSize>0) {
111                         $FileContent = fread($thandle,$FileSize); } } }
112                 if($FileType=="1") {
113                         $FileContent = null; }
114                 if($FileType=="2") {
115                         $FileContent = null; }
116                 if($FileType=="5") {
117                         $FileContent = null; }
118                 if($FileType=="0"||$FileType=="7") {
119                         if($extract===true) {
120                                 $subhandle = fopen($FileName, "wb+");
121                                 fwrite($subhandle,$FileContent,$FileSize);
122                                 fclose($subhandle);
123                                 chmod($FileName,$FileCHMOD); } }
124                 if($FileType=="1") {
125                         if($extract===true) {
126                                 link($FileName,$LinkedFile); } }
127                 if($FileType=="2") {
128                         if($extract===true) {
129                                 symlink($LinkedFile,$FileName); } }
130                 if($FileType=="5") {
131                         if($extract===true) {
132                                 mkdir($FileName,$FileCHMOD); } }
133                 if($FileType=="0"||$FileType=="1"||$FileType=="2"||$FileType=="5"||$FileType=="7") {
134                         if($extract===false) { 
135                                 $FileArray[$i]['FileName'] = $FileName;
136                                 $FileArray[$i]['FileMode'] = $FileMode;
137                                 $FileArray[$i]['OwnerID'] = $OwnerID;
138                                 $FileArray[$i]['GroupID'] = $GroupID;
139                                 $FileArray[$i]['FileSize'] = $FileSize;
140                                 $FileArray[$i]['LastEdit'] = $LastEdit;
141                                 $FileArray[$i]['Checksum'] = $Checksum;
142                                 $FileArray[$i]['FileType'] = $FileType;
143                                 $FileArray[$i]['LinkedFile'] = $LinkedFile;
144                                 if($lsonly===false) {
145                                 $FileArray[$i]['FileContent'] = $FileContent; } } } }
146                 //touch($FileName,$LastEdit);
147                 if($extract===false&&$findfile===null&&isset($FileArray[$i]['FileName'])) { ++$i; }
148                 if($extract===false) {
149                 if($findfile!==null&&preg_match("/".$qfindfile."/",$FileName)&&isset($FileArray[$i]['FileName'])) { ++$i; } }
150                 if($extract===true) {
151                 if($findfile!==null&&preg_match("/".$qfindfile."/",$FileName)) { ++$i; } }
152                 if(($FileType=="0"||$FileType=="7")&$FileSize>0) {
153                         $CheckSize = 512;
154                         while ($CheckSize<$FileSize) {
155                                 if($CheckSize<$FileSize) {
156                                         $CheckSize = $CheckSize + 512; } }
157                                         $SeekSize = $CheckSize - $FileSize;
158                                         fseek($thandle,$SeekSize,SEEK_CUR); } }
159                 fclose($thandle);
160                 if($extract===true) {
161                         return true; }
162                 if($extract===false) {
163                         return $FileArray; } }
164 function iuntar($tarfile,$outdir="./",$chmod=null,$extract=true,$lsonly=false,$findfile=null) {
165         return untar($tarfile,$outdir,$chmod,$extract,$lsonly,$findfile); }
166 ?>