3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
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.
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
15 $FileInfo: untar.php - Last Update: 11/14/2011 Ver 4.7 - Author: cooldude2k $
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) {
27 if($lsonly!==true&&$lsonly!==false) {
32 if($outdir!=""&&!file_exists($outdir)) {
33 mkdir($outdir,0777); } }
34 $thandle = fopen($tarfile, "rb");
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)) {
43 while (ftell($thandle)<$TarSizeEnd) {
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));
73 $FileCHMOD = octdec("0".substr($FileMode,-3)); }
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));
91 $FileCHMOD = octdec("0".substr($FileMode,-3)); }
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") {
105 fseek($thandle,$FileSize,SEEK_CUR); }
106 if($lsonly===false) {
107 // Empty files support added by Josep Sanz Campderros
111 $FileContent = fread($thandle,$FileSize); } } }
113 $FileContent = null; }
115 $FileContent = null; }
117 $FileContent = null; }
118 if($FileType=="0"||$FileType=="7") {
119 if($extract===true) {
120 $subhandle = fopen($FileName, "wb+");
121 fwrite($subhandle,$FileContent,$FileSize);
123 chmod($FileName,$FileCHMOD); } }
125 if($extract===true) {
126 link($FileName,$LinkedFile); } }
128 if($extract===true) {
129 symlink($LinkedFile,$FileName); } }
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) {
154 while ($CheckSize<$FileSize) {
155 if($CheckSize<$FileSize) {
156 $CheckSize = $CheckSize + 512; } }
157 $SeekSize = $CheckSize - $FileSize;
158 fseek($thandle,$SeekSize,SEEK_CUR); } }
160 if($extract===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); }
167 //Check if zlib is loaded
168 if(extension_loaded("zlib")) {
169 function gunzip($infile, $outfile) {
171 $zp = gzopen($infile, "r");
173 $string .= gzread($zp, 4096);
175 $fp = fopen($outfile, "w");
176 fwrite($fp, $string, strlen($string));
180 function gunzip2($infile, $outfile) {
181 $string = implode("", gzfile($infile));
182 $fp = fopen($outfile, "w");
183 fwrite($fp, $string, strlen($string));