Table of Contents
php uses the ZipArchive function to compress and decompress files.
Home Backend Development PHP Tutorial PHP uses the ZipArchive function to compress and decompress files, _PHP tutorial

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

Jul 12, 2016 am 09:06 AM
php ziparchive compression

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles