Tar: predict total archive size and send the header

This commit is contained in:
Tom van der Woerdt 2013-07-17 19:07:10 +02:00
parent 89acf0cafc
commit e815f4d070

View File

@ -83,20 +83,34 @@ class Archive {
$root_path = $this->app->get_root_abs_path();
// Build a list of filesizes so we can predict the total size
$filesizes = array();
$total_size = 0;
foreach (array_values($this->files) as $file) {
// TAR supports filenames up to 253 chars, but the name should be split ubti a 154-byte prefix and 99-byte name
if (substr($file, 0, strlen($root_path)) != $root_path) {
$file = $this->app->get_abs_path().'/'.$file;
}
if (!file_exists($file)) continue;
$size = filesize($file);
$filesizes[$file] = $size;
$total_size += 512 + $size + (512 - ($size % 512));
}
header('Content-Length: '.$total_size);
foreach (array_keys($filesizes) as $file) {
// TAR supports filenames up to 253 chars, but the name should be split ubti a 154-byte prefix and 99-byte name
$local_filename = normalize_path(substr($file, strlen($root_path) + 1));
$filename_parts = array('', substr($local_filename, -99));
if (strlen($local_filename) > 99) $filename_parts[0] = substr($local_filename, 0, -99);
if (strlen($filename_parts[0]) > 154) $filename_parts[0] = substr($filename_parts[0], -154);
$size = filesize($file);
$size = $filesizes[$file];
$file_header =
str_pad($filename_parts[1], 100, "\0") // first filename part