Home > Backend Development > PHP Tutorial > php中运用zip

php中运用zip

WBOY
Release: 2016-06-13 13:11:18
Original
848 people have browsed it

php中使用zip
  在PHP中,可以使用PHP自己的ZIP功能去为某些文件打包和解压,这点来说比其他语言
都很方便,方法小结如下:

1 打开PHP.INI中的扩展extension,启用(PHP 5.2以后有这个功能)
  extension=php_zip.dll

2 压缩文件
  



// Creating object of the ZipArchive
 $zip = new ZipArchive();
 
 $ow = 1;
 $file= "c:\\master.zip";
 if($zip->open($file,$ow?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE)
 {
   //增加一个文件
   $zip->addFile("c:\\master.txt");
   
   //a1.txt会被保存加压到a2.txt
   $zip->addFile("c:\\a1.txt", "c:\\a2.txt");
   
   $zip->addFile("c:\\a3.txt");
   
   //关闭 
   $zip->close();
   
  }
 ?>
Copy after login


3 解压
 
 $zip = new ZipArchive();

 //打开master.zip 
 if ($zip->open("master.zip") === TRUE) 
 {
   //解压的路径
   $zip->extractTo("/path/to/folder/");
   $zip->close();
 }

 // 只解压一个文件 
 // Open master.zip for extracting single files
 if ($zip->open("master.zip") === TRUE) 
 {
   // Will extract only fonts.css from master.zip to given path.
   $zip->extractTo("/path/to/folder/","fonts.css");
   $zip->close();
 }

 //解压多个文件 
 if ($zip->open("master.zip") === TRUE) 
 {
      $files = array("fonts.css","master.css");
   $zip->extractTo("/path/to/folder/",$files);
   $zip->close();
 }

Copy after login

 
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