<?PHP
	include('tar.php');

	$SUPPORTED_FILES = array("bz2" => "bz2", "zip" => "zip", "gz" => "gz", "tar" => "tar");

	function view_bz2($filepath)
	{
	}

	function view_zip($filepath)
	{
		$zip = zip_open($filepath);
		if($zip)
		{
			while ($zip_entry = zip_read($zip))
			{
				echo "Name:               " . zip_entry_name($zip_entry) . "<br>\n";
				echo "Actual Filesize:    " . zip_entry_filesize($zip_entry) . "<br>\n";
				echo "Compressed Size:    " . zip_entry_compressedsize($zip_entry) . "<br>\n";
				echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "<br>\n";

				if (zip_entry_open($zip, $zip_entry, "r"))
				{
					echo "File Contents:<br>\n";
					$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
					echo "$buf<br>\n";
					zip_entry_close($zip_entry);
				}
			}
			echo "<br>\n";
		}
		zip_close($zip);
	}

	function view_gz($filepath)
	{
		// Open the zlib compressed file for reading only
		$gz = gzopen($filepath, "r");

		// Build array of files in the archive
//		$files = array();
//		while($line = gzgets($gz, 10240))
//		{
//			$pattern = '/\b([\/\w\s\.-]+)\b\W*\b([0-7]{7})\b.\d{7}\b/';
//			preg_match_all($pattern, $line, $matches);
//			$files = array_merge($files, $matches[1]);
//		}

		foreach($files as $file)
		{
			echo "Found files: " . $file . "<br>\n";
		}
		gzclose($gz);
	}

	function view_tar($filepath)
	{
		$tp = taropen($filepath, "r");
		examine_tar($tp);
		tarclose($tp);
	}

	function print_contents($filepath)
	{
		global $SUPPORTED_FILES;
		$pattern = '/\.(\w+)$/';
		$hits = preg_match($pattern, $filepath, $matches);
		if($hits != 0)
		{
			$file_type = $matches[1];
			if($SUPPORTED_FILES[$file_type])
			{
				if($file_type == $SUPPORTED_FILES[bz2])
					view_bz2($filepath);
				else if($file_type == $SUPPORTED_FILES[zip])
					view_zip($filepath);
				else if($file_type == $SUPPORTED_FILES[gz])
					view_gz($filepath);
				else if($file_type == $SUPPORTED_FILES[tar])
					view_tar($filepath);
				else
					echo "No-no!<br>\n";
			}
			else	// Terminate execution
				die("No support for this file type.<br>\n");
		}
		else
			echo "No matches found<br>\n";
	}

	print_contents($_GET[file]);
PHP?>
