Skip to content

Commit f931cad

Browse files
committed
Tar::readCurrentEntry(): recognize end of file properly
1 parent 6136d0a commit f931cad

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Tar.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Tar extends Archive
2424
protected $closed = true;
2525
protected $writeaccess = false;
2626
protected $position = 0;
27+
protected $contentUntil = 0;
2728
protected $skipUntil = 0;
2829

2930
/**
@@ -121,6 +122,7 @@ public function yieldContents()
121122
continue;
122123
}
123124

125+
$this->contentUntil = $this->position + $header['size'];
124126
$this->skipUntil = $this->position + ceil($header['size'] / 512) * 512;
125127

126128
yield $this->header2fileinfo($header);
@@ -134,9 +136,22 @@ public function yieldContents()
134136
$this->close();
135137
}
136138

139+
/**
140+
* Reads content of a current archive entry.
141+
*
142+
* Works only when iterating trough the archive using the generator returned
143+
* by the yieldContents().
144+
*
145+
* @param int $length maximum number of bytes to read
146+
*
147+
* @return string
148+
*/
137149
public function readCurrentEntry($length = PHP_INT_MAX)
138150
{
139-
$length = min($length, $this->skipUntil - $this->position);
151+
$length = (int) min($length, $this->contentUntil - $this->position);
152+
if ($length === 0) {
153+
return '';
154+
}
140155
return $this->readbytes($length);
141156
}
142157

@@ -790,3 +805,4 @@ static public function numberEncode($value, $length)
790805
return $encoded;
791806
}
792807
}
808+

0 commit comments

Comments
 (0)