Skip to content

Commit f15ef3a

Browse files
committed
Tar::addFile(): use larger read buffer for better performance
1 parent f931cad commit f15ef3a

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Tar.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,27 @@ public function addFile($file, $fileinfo = '')
319319
throw new ArchiveIOException('Could not open file for reading: ' . $file);
320320
}
321321
while (!feof($fp)) {
322-
$data = fread($fp, 512);
323-
$read += strlen($data);
322+
// try to read 1 MB (512 bytes is unperformant)
323+
$data = fread($fp, 1048576);
324324
if ($data === false) {
325325
break;
326326
}
327327
if ($data === '') {
328328
break;
329329
}
330-
$packed = pack("a512", $data);
331-
$this->writebytes($packed);
330+
$dataLen = strlen($data);
331+
$read += $dataLen;
332+
// how much of data read fully fills 512-byte blocks?
333+
$passLen = ($dataLen >> 9) << 9;
334+
if ($passLen === $dataLen) {
335+
// all - just write the data
336+
$this->writebytes($data);
337+
} else {
338+
// directly write what fills 512-byte blocks fully
339+
$this->writebytes(substr($data, 0, $passLen));
340+
// pad the reminder to 512 bytes
341+
$this->writebytes(pack("a512", substr($data, $passLen)));
342+
}
332343
}
333344
fclose($fp);
334345

0 commit comments

Comments
 (0)