Home > Backend Development > PHP Tutorial > How to decompress and compress PHP files? (code example)

How to decompress and compress PHP files? (code example)

青灯夜游
Release: 2023-04-06 16:56:02
Original
10396 people have browsed it

How to decompress php files: first check whether the compressed package to be decompressed exists; then check whether the target path exists; finally, use the "$zip->extractTo($dest);" method to decompress the file.

How to decompress and compress PHP files? (code example)

The following article will introduce to you how to use PHP to compress and decompress files. It has certain reference value. Now I will share it with you. Friends in need can refer to it.

Note: PHP compression and decompression files require zip extension---ZipArchive class

PHP ZipArchive class can be used for compression and decompression. If it doesn't exist, you may need to install the class.

Starting with PHP 5.3, this extension is built-in. Prior to this, Windows users needed to enable php_zip.dll in php.ini to use its functionality.

Enable steps:

1. Open the php.ini file and add

extension=php_zip.dll
Copy after login

2. After saving, restart Apache or other servers.

How to decompress PHP files?

/**
 * 解压缩
 * @method unzip_file
 * @param  string     $zipName 压缩包名称
 * @param  string     $dest    解压到指定目录
 * @return boolean              true|false
 */
function unzip_file(string $zipName,string $dest){
  //检测要解压压缩包是否存在
  if(!is_file($zipName)){
    return false;
  }
  //检测目标路径是否存在
  if(!is_dir($dest)){
    mkdir($dest,0777,true);
  }
  $zip=new ZipArchive();
  if($zip->open($zipName)){
    $zip->extractTo($dest);
    $zip->close();
    return true;
  }else{
    return false;
  }
}
Copy after login

How does PHP compress files?

Example 1: Compress a single file

/**
 * 压缩单个文件
 * @method zip_file
 * @param  string   $filename 文件名
 * @return boolean             true|false
 */
function zip_file(string $filename){
  if(!is_file($filename)){
    return false;
  }
  $zip=new ZipArchive();
  $zipName=basename($filename).'.zip';
  //打开指定压缩包,不存在则创建,存在则覆盖
  if($zip->open($zipName,ZipArchive::CREATE|ZipArchive::OVERWRITE)){
    //将文件添加到压缩包中
    if($zip->addFile($filename)){
      $zip->close();
      @unlink($filename);
      return true;
    }else{
      return false;
    }
  }else{
    return false;
  }
}
// var_dump(zip_file('22.txt'));
// func_get_args
// test1.zip
Copy after login

Example 2: Compress multiple files

/**
 * 多文件压缩
 * @method zip_files
 * @param  string    $zipName 压缩包的名称,.zip结尾
 * @param  string     $files   需要压缩文件名,可以是多个
 * @return boolean             true|false
 */
function zip_files(string $zipName,...$files){
  //检测压缩包名称是否正确
  $zipExt=strtolower(pathinfo($zipName,PATHINFO_EXTENSION));
  if('zip'!==$zipExt){
    return false;
  }
  $zip=new ZipArchive();
  if($zip->open($zipName,ZipArchive::CREATE|ZipArchive::OVERWRITE)){
    foreach($files as $file){
      if(is_file($file)){
        $zip->addFile($file);
      }
    }
    $zip->close();
    return true;
  }else{
    return false;
  }
}
// var_dump(zip_files('test1.zip','22.txt'));
// var_dump(zip_files('test2.zip','doUpload.php','downLoad.html','upload.html'));
Copy after login

Related video tutorials Recommended: "PHP Tutorial"

The above is the detailed content of How to decompress and compress PHP files? (code example). For more information, please follow other related articles on the PHP Chinese website!

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