Heim > php教程 > php手册 > PHP压缩、解压缩ZIP

PHP压缩、解压缩ZIP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-06 19:35:02
Original
1179 Leute haben es durchsucht

PHP压缩、解压缩ZIP 无 ?php/** * @version 1.0 * @date 2014-08-11 * @author 十七号 xialeistudio@gmail.com * @license MIT * 压缩、解压缩类 */class Zip{/** * 打包 * @param $path * @param $save */public static function archive($path, $save){$zip

PHP压缩、解压缩ZIP
<?php

/**
 * @version 1.0
 * @date 2014-08-11
 * @author 十七号 <xialeistudio@gmail.com>
 * @license MIT
 * 压缩、解压缩类
 */
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();
		}
	}
}
Nach dem Login kopieren
Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
PHP-Datenerfassung?
Aus 1970-01-01 08:00:00
0
0
0
PHP-Erweiterung intl
Aus 1970-01-01 08:00:00
0
0
0
Wie man PHP gut lernt
Aus 1970-01-01 08:00:00
0
0
0
Mehrere PHP-Versionen
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage