Home  >  Article  >  Backend Development  >  Implementation method of decompressing zip compressed package in php to specified directory

Implementation method of decompressing zip compressed package in php to specified directory

小云云
小云云Original
2018-01-24 09:28:435954browse

This article mainly shares an example of decompressing the contents of a zip package to a specified directory in PHP. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Directory structure:

test

test/index.php
test/test_zip.zip
test/test_zip

<span style="font-size:14px;"><?php
	header(&#39;Content-type:text/html;charset=utf-8&#39;);
	$filename = &#39;test_zip.zip&#39;;
	$path = &#39;./test_zip.zip&#39;;
	$dir = &#39;test_zip&#39;;
	if(!is_dir($dir)) {
		mkdir($dir, 0777, true);//创建目录保存解压内容
	}
	if(file_exists($filename)) {
		$resource = zip_open($filename);
		while($zip = zip_read($resource)) {
			if(zip_entry_open($resource, $zip)) {
		$file_content = zip_entry_name($zip);//获得文件名,mac压缩成zip,解压需要过滤资源库隐藏文件
				$file_name = substr($file_content, strrpos($file_content, &#39;/&#39;) +1);
				if(!is_dir($file_name) && $file_name) {
					$save_path = $dir .&#39;/&#39;. $file_name;
					if(file_exists($save_path)) {
					echo &#39;文件夹内已存在文件 "&#39; . $file_name . &#39;" <pre />';
					}else {
						echo $file_name . '<pre />';	
						$file_size = zip_entry_filesize($zip);
						$file = zip_entry_read($zip, $file_size);
						file_put_contents($save_path, $file);
						zip_entry_close($zip);
					}
					
				}
			}
		}
		zip_close($resource);
	}</span>

Have you learned it yet? If you find it useful, try it now.

Related recommendations:

How to use PHP to display the images in the compressed package without decompressing it

php tutorial :How to decompress the compressed package

Generate zip compressed file through php, support file and compressed package path search

The above is the detailed content of Implementation method of decompressing zip compressed package in php to specified directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn