From e815f4d070919a1e8b43f092ae6dacd13c9b68fb Mon Sep 17 00:00:00 2001 From: Tom van der Woerdt Date: Wed, 17 Jul 2013 19:07:10 +0200 Subject: [PATCH] Tar: predict total archive size and send the header --- src/_h5ai/server/php/inc/Archive.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/_h5ai/server/php/inc/Archive.php b/src/_h5ai/server/php/inc/Archive.php index 8377b79e..d253aeda 100644 --- a/src/_h5ai/server/php/inc/Archive.php +++ b/src/_h5ai/server/php/inc/Archive.php @@ -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