File "zip.class.php"

Full Path: /www/wwwroot/shphe-en.com/wp-content/plugins/t9_eshopimport/inc/zip.class.php
File size: 2.17 KB
MIME-type: --
Charset: 8 bit

<?php
class Zip {
	/**
	 * 
	 * 
	 * @param  $path 
	 * @param  $save 
	 */
	public static function archive($path, $save) {
		$zip = new ZipArchive();
		if ($zip -> open($save, ZipArchive :: OVERWRITE) === true) {
			self :: addZip($path, $zip);
			$zip -> close();
		} 
	} 

	/**
	 * ļļеzip
	 * 
	 * @param string $path 
	 * @param ZipArchive $zip 
	 */
	private static function addZip($path, $zip) {
		$handler = opendir($path);
		while (($file = readdir($handler)) !== false) {
			if ($file != '.' && $file != '..') {
				if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
					self :: addZip($path . DIRECTORY_SEPARATOR . $file, $zip);
				} else {
					$zip -> addFile($path . DIRECTORY_SEPARATOR . $file);
				} 
			} 
		} 
		closedir($handler);
	} 

	/**
	 * ѹļ
	 * 
	 * @param string $file ѹļ·
	 * @param string $path ѹ·ΪļΪ·
	 */
	public static function extra($file, $path = null) {
		if (!isset($path)) {
			$array = explode('.', $file);
			$path = reset($array);
		} 

		$zip = new ZipArchive();
		if ($zip -> open($file) === true) {
			$zip -> extractTo($path);
			$zip -> close();
		} 
	} 
	private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
		$handle = opendir($folder);
		while (false !== $f = readdir($handle)) {
			if ($f != '.' && $f != '..') {
				$filePath = "$folder/$f"; 
				// Remove prefix from file path before add to zip.
				$localPath = substr($filePath, $exclusiveLength);
				if (is_file($filePath)) {
					$zipFile -> addFile($filePath, $localPath);
				} elseif (is_dir($filePath)) {
					$zipFile -> addEmptyDir($localPath);
					self :: folderToZip($filePath, $zipFile, $exclusiveLength);
				} 
			} 
		} 
		closedir($handle);
	} 

	public static function zipDir($sourcePath, $outZipPath) {
		$pathInfo = pathInfo($sourcePath);
		$parentPath = $pathInfo['dirname'];
		$dirName = $pathInfo['basename'];
		$sourcePath = $parentPath . '/' . $dirName; //ֹ'folder' ļвbug 
		$z = new ZipArchive();
		$z -> open($outZipPath, ZIPARCHIVE :: CREATE); //zipļ 
		$z -> addEmptyDir($dirName); //ļ 
		self :: folderToZip($sourcePath, $z, strlen("$parentPath/"));
		$z -> close();
	} 
}