#!/usr/bin/perl

# Script to calculate total size of files in a given path inside
# a tar archive.  Runs tar -tvf on the file and counts up the output
# filesizes.
#
# Returns the path's total size in kilobytes on stdout

$pathsize=0;

open (TAR, "tar -tvf $ARGV[0] $ARGV[1] |" );
while ($line = <TAR>)
{
	@linefields = split(/ +/, $line);
	$filesize=$linefields[2];
	if ($filesize == 0) {
		$pathsize+=$filesize; 
	} if ( $filesize =~ ',' ) {
		$pathsize+=0;
	} else {
		$fileblocks = $filesize / 2096;
		$truesize = ( $fileblocks + 1 ) * 2096;
		$pathsize+=$truesize;
	}
}
close (TAR);

$pathsize = ($pathsize / 1024);
printf ("%d\n", $pathsize);
