PHP uses the ZipArchive function to compress and decompress files, _PHP tutorial

WBOY
Release: 2016-07-12 09:06:19
Original
914 people have browsed it

php uses the ZipArchive function to compress and decompress files.

PHP ZipArchive is an extension class that comes with PHP, which can easily compress and decompress ZIP files. Before use, first Make sure that the PHP ZIP extension has been enabled. The specific opening method will not be discussed here. Methods for enabling PHP expansion on different platforms are available online. If you have any questions, please feel free to communicate. Here are some common examples of using php zipArchive to compress and decompress files for reference.
1. Unzip the zip file

$zip=new ZipArchive;//新建一个ZipArchive的对象 
  if($zip->open('test.zip')===TRUE){ 
  $zip->extractTo('images');//假设解压缩到在当前路径下images文件夹内 
  $zip->close();//关闭处理的zip文件 
} 
Copy after login

2. Compress the file into a zip file

$zip=new ZipArchive; 
if($zip->open('test.zip',ZipArchive::OVERWRITE)===TRUE){ 
  $zip->addFile('image.txt');//假设加入的文件名是image.txt,在当前路径下 
  $zip->close(); 
} 
Copy after login

3. Add file append content to zip file

$zip=new ZipArchive; 
$res=$zip->open('test.zip',ZipArchive::CREATE); 
if($res===TRUE){ 
  $zip->addFromString('test.txt','file content goes here'); 
  $zip->close(); 
  echo 'ok'; 
}else{ 
  echo 'failed'; 
} 
Copy after login

4. Pack the folder into a zip file

function addFileToZip($path,$zip){ 
  $handler=opendir($path); //打开当前文件夹由$path指定。 
  while(($filename=readdir($handler))!==false){ 
    if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..',不要对他们进行操作 
      if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归 
        addFileToZip($path."/".$filename, $zip); 
      }else{ //将文件加入zip对象 
        $zip->addFile($path."/".$filename); 
      } 
    } 
  } 
  @closedir($path); 
} 
$zip=new ZipArchive(); 
if($zip->open('images.zip', ZipArchive::OVERWRITE)=== TRUE){ 
  addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法 
  $zip->close(); //关闭处理的zip文件 
} 
Copy after login

The above are four different situations in which PHP implements file compression and decompression. There may be other situations that have not been completed. They will be updated in subsequent articles. I hope this article will be helpful to everyone's learning.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1065577.htmlTechArticlephp uses the ZipArchive function to compress and decompress files. PHP ZipArchive is an extension class that comes with PHP, which can be easily To compress and decompress ZIP files, you must first ensure PHP...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!