<?php
/**
* @package   ZOO Component
* @file      archive.php
* @version   2.4.10 June 2011
* @author    YOOtheme http://www.yootheme.com
* @copyright Copyright (C) 2007 - 2011 YOOtheme GmbH
* @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
*/

/*
	Class: ArchiveHelper
		Archive helper class
*/
class ArchiveHelper extends AppHelper {

	/*
		Function: Constructor
			Class Constructor.
	*/
	public function __construct($app) {
		parent::__construct($app);

		// increase memory limit
		@ini_set('memory_limit', '256M');

		$app->loader->register('PclZip', 'libraries:pcl/pclzip.lib.php');

	}

	/*
		Function: open
			Open archive file

		Returns:
			Mixed
	*/
	public function open($file, $format = 'zip') {

		// auto-detect format
		if (!$format) {
			$format = $this->format($file);
		}

		// create archive object
		if ($format == 'zip') {
			return new PclZip($file);
		}

		return null;
	}

	/*
		Function: format
			Get archive format based on filename

		Returns:
			Mixed
	*/
	public function format($file) {

		// detect .zip format
		if (preg_match('/\.zip$/i', $file)) {
			return 'zip';
		}

		// detect .tar format
		if (preg_match('/\.tar$|\.tar\.gz$|\.tgz$|\.tar\.bz2$|\.tbz2$/i', $file)) {
			return 'tar';
		}

		return null;
	}

}