|
15 | 15 | */
|
16 | 16 | class Tar extends Archive
|
17 | 17 | {
|
| 18 | + const READ_CHUNK_SIZE = 1048576; // 1MB |
18 | 19 |
|
19 | 20 | protected $file = '';
|
20 | 21 | protected $comptype = Archive::COMPRESS_AUTO;
|
@@ -319,8 +320,8 @@ public function addFile($file, $fileinfo = '')
|
319 | 320 | throw new ArchiveIOException('Could not open file for reading: ' . $file);
|
320 | 321 | }
|
321 | 322 | while (!feof($fp)) {
|
322 |
| - // try to read 1 MB (512 bytes is unperformant) |
323 |
| - $data = fread($fp, 1048576); |
| 323 | + // for performance reasons read bigger chunks at once |
| 324 | + $data = fread($fp, self::READ_CHUNK_SIZE); |
324 | 325 | if ($data === false) {
|
325 | 326 | break;
|
326 | 327 | }
|
@@ -375,8 +376,11 @@ public function addData($fileinfo, $data)
|
375 | 376 | $fileinfo->setSize($len);
|
376 | 377 | $this->writeFileHeader($fileinfo);
|
377 | 378 |
|
378 |
| - for ($s = 0; $s < $len; $s += 512) { |
379 |
| - $this->writebytes(pack("a512", substr($data, $s, 512))); |
| 379 | + // write directly everything but the last block which needs padding |
| 380 | + $passLen = ($len >> 9) << 9; |
| 381 | + $this->writebytes(substr($data, 0, $passLen)); |
| 382 | + if ($passLen < $len) { |
| 383 | + $this->writebytes(pack("a512", substr($data, $passLen, 512))); |
380 | 384 | }
|
381 | 385 |
|
382 | 386 | if (is_callable($this->callback)) {
|
|
0 commit comments