135 $unsigned_chksum =
'';
136 for (
$i = 0;
$i < 512;
$i++) {
137 $unsigned_chksum += ord($bytestring[
$i]);
139 for (
$i = 0;
$i < 8;
$i++) {
140 $unsigned_chksum -= ord($bytestring[148 +
$i]);
141 $unsigned_chksum += ord(
' ') * 8;
143 return $unsigned_chksum;
155 $position = strpos($string, chr(0));
156 return substr($string, 0, $position);
168 $tar_length = strlen($this->tar_file);
171 while ($main_offset < $tar_length) {
173 if (substr($this->tar_file, $main_offset, 512) == str_repeat(chr(0), 512))
178 $file_mode = substr($this->tar_file, $main_offset + 100, 8);
180 $file_uid = octdec(substr($this->tar_file, $main_offset + 108, 8));
182 $file_gid = octdec(substr($this->tar_file, $main_offset + 116, 8));
184 $file_size = octdec(substr($this->tar_file, $main_offset + 124, 12));
186 $file_time = octdec(substr($this->tar_file, $main_offset + 136, 12));
188 $file_chksum = octdec(substr($this->tar_file, $main_offset + 148, 6));
197 $file_contents = substr($this->tar_file, $main_offset + 512, $file_size);
211 if ($file_size > 0) {
215 $activeFile =& $this->files[];
217 $activeFile[
"name"] = $file_name;
218 $activeFile[
"mode"] = $file_mode;
219 $activeFile[
"size"] = $file_size;
220 $activeFile[
"time"] = $file_time;
221 $activeFile[
"user_id"] = $file_uid;
222 $activeFile[
"group_id"] = $file_gid;
223 $activeFile[
"user_name"] = $file_uname;
224 $activeFile[
"group_name"] = $file_gname;
225 $activeFile[
"checksum"] = $file_chksum;
226 $activeFile[
"file"] = $file_contents;
229 $this->numDirectories ++;
231 $activeDir =& $this->directories[];
233 $activeDir[
"name"] = $file_name;
234 $activeDir[
"mode"] = $file_mode;
235 $activeDir[
"time"] = $file_time;
236 $activeDir[
"user_id"] = $file_uid;
237 $activeDir[
"group_id"] = $file_gid;
238 $activeDir[
"user_name"] = $file_uname;
239 $activeDir[
"group_name"] = $file_gname;
240 $activeDir[
"checksum"] = $file_chksum;
243 $main_offset += 512 + (ceil($file_size / 512) * 512);
264 $this->tar_file = fread($fp, filesize(
$filename));
267 if ($this->tar_file[0] == chr(31) && $this->tar_file[1] == chr(139) && $this->tar_file[2] == chr(8)) {
268 if (!function_exists(
'gzinflate')) {
271 $this->isGzipped =
true;
272 $this->tar_file = gzinflate(substr($this->tar_file, 10, - 4));
289 unset($this->tar_file);
291 if ($this->numDirectories > 0) {
292 foreach ($this->directories as $key => $information) {
296 $header .= str_pad($information[
"name"], 100, chr(0));
297 $header .= str_pad(decoct($information[
"mode"]), 7,
"0", STR_PAD_LEFT) . chr(0);
298 $header .= str_pad(decoct($information[
"user_id"]), 7,
"0", STR_PAD_LEFT) . chr(0);
299 $header .= str_pad(decoct($information[
"group_id"]), 7,
"0", STR_PAD_LEFT) . chr(0);
300 $header .= str_pad(decoct(0), 11,
"0", STR_PAD_LEFT) . chr(0);
301 $header .= str_pad(decoct($information[
"time"]), 11,
"0", STR_PAD_LEFT) . chr(0);
302 $header .= str_repeat(
" ", 8);
304 $header .= str_repeat(chr(0), 100);
305 $header .= str_pad(
"ustar", 6, chr(32));
306 $header .= chr(32) . chr(0);
307 $header .= str_pad(
"", 32, chr(0));
308 $header .= str_pad(
"", 32, chr(0));
309 $header .= str_repeat(chr(0), 8);
310 $header .= str_repeat(chr(0), 8);
311 $header .= str_repeat(chr(0), 155);
312 $header .= str_repeat(chr(0), 12);
315 for (
$i = 0;
$i < 6;
$i ++) {
316 $header[(148 +
$i)] = substr($checksum,
$i, 1);
318 $header[154] = chr(0);
319 $header[155] = chr(32);
321 $this->tar_file .= $header;
325 if ($this->numFiles > 0) {
326 $this->tar_file =
'';
327 foreach($this->files as $key => $information) {
331 $header = str_pad($information[
"name"], 100, chr(0));
332 $header .= str_pad(decoct($information[
"mode"]), 7,
"0", STR_PAD_LEFT) . chr(0);
333 $header .= str_pad(decoct($information[
"user_id"]), 7,
"0", STR_PAD_LEFT) . chr(0);
334 $header .= str_pad(decoct($information[
"group_id"]), 7,
"0", STR_PAD_LEFT) . chr(0);
335 $header .= str_pad(decoct($information[
"size"]), 11,
"0", STR_PAD_LEFT) . chr(0);
336 $header .= str_pad(decoct($information[
"time"]), 11,
"0", STR_PAD_LEFT) . chr(0);
337 $header .= str_repeat(
" ", 8);
339 $header .= str_repeat(chr(0), 100);
340 $header .= str_pad(
"ustar", 6, chr(32));
341 $header .= chr(32) . chr(0);
342 $header .= str_pad($information[
"user_name"], 32, chr(0));
343 $header .= str_pad($information[
"group_name"], 32, chr(0));
344 $header .= str_repeat(chr(0), 8);
345 $header .= str_repeat(chr(0), 8);
346 $header .= str_repeat(chr(0), 155);
347 $header .= str_repeat(chr(0), 12);
350 for (
$i = 0;
$i < 6;
$i++) {
351 $header[(148 +
$i)] = substr($checksum,
$i, 1);
353 $header[154] = chr(0);
354 $header[155] = chr(32);
356 $file_contents = str_pad($information[
"file"], (ceil($information[
"size"] / 512) * 512), chr(0));
358 $this->tar_file .= $header . $file_contents;
362 $this->tar_file .= str_repeat(chr(0), 512);
375 unset($this->filename);
376 unset($this->isGzipped);
377 unset($this->tar_file);
379 unset($this->directories);
380 unset($this->numFiles);
381 unset($this->numDirectories);
416 if ($this->numFiles > 0) {
417 foreach ($this->files as $key => $information) {
434 if ($this->numDirectories > 0) {
435 foreach ($this->directories as $key => $information) {
436 if ($information[
'name'] ==
$dirname) {
452 if ($this->numFiles > 0) {
453 foreach ($this->files as $key => $information) {
470 if ($this->numDirectories > 0) {
471 foreach ($this->directories as $key => $information) {
472 if ($information[
'name'] ==
$dirname) {
494 $this->numDirectories++;
495 $activeDir =& $this->directories[];
497 $activeDir[
'mode'] = $file_information[
'mode'];
498 $activeDir[
'time'] = $file_information[
'time'];
499 $activeDir[
'user_id'] = $file_information[
'uid'];
500 $activeDir[
'group_id'] = $file_information[
'gid'];
501 $activeDir[
'checksum'] = $checksum;
531 $file_contents = fread($fp, filesize(
$filename));
535 $activeFile =& $this->files[];
537 $activeFile[
'mode'] = $file_information[
'mode'];
538 $activeFile[
'user_id'] = $file_information[
'uid'];
539 $activeFile[
'group_id'] = $file_information[
'gid'];
540 $activeFile[
'size'] = $file_information[
'size'];
541 $activeFile[
'time'] = $file_information[
'mtime'];
542 $activeFile[
'checksum'] = isset($checksum) ? $checksum :
'';
543 $activeFile[
'user_name'] =
'';
544 $activeFile[
'group_name'] =
'';
545 $activeFile[
'file'] = trim($file_contents);
557 if ($this->numFiles > 0) {
558 foreach ($this->files as $key => $information) {
561 unset($this->files[$key]);
577 if ($this->numDirectories > 0) {
578 foreach ($this->directories as $key => $information) {
579 if ($information[
'name'] ==
$dirname) {
580 $this->numDirectories--;
581 unset($this->directories[$key]);
596 if (!$this->filename) {
600 $this->
toTar($this->filename, $this->isGzipped);
617 $this->__generateTar();
621 if (!function_exists(
'gzencode')) {
624 $file = gzencode($this->tar_file);
648 $this->__generateTar();
652 if (!function_exists(
'gzencode')) {
655 $file = gzencode($this->tar_file);