Merge #20206, #5803: more efficient algorithm that doesn't truncate output.

(No idea why test_tarfile is listed as changed...it isn't.)