美文网首页
php对目录下的子目录及文件进行压缩,并解压

php对目录下的子目录及文件进行压缩,并解压

作者: 程序员Ameng | 来源:发表于2020-12-05 16:04 被阅读0次

创建压缩类文件 zip.php

<?php

class Zip{
  
  /**
   * Zip a folder (include itself).
   * Usage:
   *   Zip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
   *
   * @param string $sourcePath Path of directory to be zip.
   * @param string $outZipPath Path of output zip file.
   */
  public static function zipDir($sourcePath, $outZipPath)
  {

    $pathInfo = self::myPathInfo($sourcePath);
    $parentPath = $pathInfo['dirname'];
    $dirName = $pathInfo['basename'];
    $sourcePath=$parentPath.'/'.$dirName;//防止传递'folder' 文件夹产生bug
    $z = new \ZipArchive();
    $z->open($outZipPath, \ZIPARCHIVE::CREATE);//建立zip文件
    $z->addEmptyDir($dirName);//建立文件夹
    self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
    $z->close();
  }

  private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
    $handle = opendir($folder);
    while (false !== $f = readdir($handle)) {
      if ($f != '.' && $f != '..') {
        $filePath = "$folder/$f";
        // Remove prefix from file path before add to zip.
        $localPath = substr($filePath, $exclusiveLength);
        if (is_file($filePath)) {
          $zipFile->addFile($filePath, $localPath);
        } elseif (is_dir($filePath)) {
          // 添加子文件夹
          $zipFile->addEmptyDir($localPath);
          self::folderToZip($filePath, $zipFile, $exclusiveLength);
        }
      }
    }
    closedir($handle);
  }

  private static function myPathInfo($filepath)  
  {  
      $pathParts = array();  
      $pathParts ['dirname'] = rtrim(substr($filepath, 0, strrpos($filepath, '/')),"/")."/";  
      $pathParts ['basename'] = ltrim(substr($filepath, strrpos($filepath, '/')),"/");  
      $pathParts ['extension'] = substr(strrchr($filepath, '.'), 1);  
      $pathParts ['filename'] = ltrim(substr($pathParts ['basename'], 0, strrpos($pathParts ['basename'], '.')),"/");  
      return $pathParts;  
  } 
}

测试

  1. 将test文件夹进行压缩,生成的文件test.zip,放入zip目录
  2. 创建test.php
<?php
    require_once('zip.php');
    $zip = new Zip();
   
    $sourceDir = 'D:/phpstudy_pro/WWW/test/zip/test';
    $outZipPath = 'D:/phpstudy_pro/WWW/test/zip/zip/test.zip';
    $zip->zipDir($sourceDir, $outZipPath);
    if(file_exists($outZipPath)){
        echo 'success';
    }else{
        echo 'fail';
    }

创建的目录结构如下

生成的结果

解压文件

<?php

$zip = new \ZipArchive();
if ($zip->open('test.zip') === TRUE){
    //假设解压缩到在当前路径下demo文件夹
    $zip->extractTo('demo');
}
$zip->close();

相关文章

  • php对目录下的子目录及文件进行压缩,并解压

    创建压缩类文件 zip.php 测试 将test文件夹进行压缩,生成的文件test.zip,放入zip目录 创建t...

  • zip tar压缩解压常用命令

    Linux下的压缩解压缩命令详解及实例-r表示递归子目录下所有文件.zip -r myfile.zip将当前目录下...

  • [Linux实用命令]-5-文件的压缩和归档

    引言 这篇文章主要介绍Linux下如何对单个文件进行压缩和解压缩,如何对一个文件夹中的多个文件进行归档压缩和解压缩...

  • linux压缩解压

    zip压缩解压 压缩文件 压缩目录 unzip解压缩 不重建文档的目录结构,把所有文件解压到同一目录下 将压缩文件...

  • Linux命令 | gzip

    gzip是在Linux上进行解压及压缩的命令 在用gzip的时候,要解压缩则要加入参数-d;一定要保证解压缩的文件...

  • linux修改压缩包内容,打包

    对包进行解压缩 tar -zxvf tar_name.tar.gz 查看压缩包中要修改的文件,并修改 cat fi...

  • python 递归解压缩zip文件

    如何进行递归的对压缩文件进行解压 在使用zipfile库解压压缩文件的时候,有时候会遇到一种情况,就是一个压缩文件...

  • Linux--tree命令

    概述 tree命令的中文意思为“树”,功能是以树形结构列出指定目录下的所有内容,包括所有文件、子目录及子目录里的目...

  • Linux下ftp服务器文件夹权限处理

    问题描述:用户无法上传文件到指定的子目录下,却可以上传到根目录下; 解决方案:修改子目录(及子目录下文件)所有者和...

  • 2018-03-25windows下配置php+composer

    下载php压缩包 解压至特定目录 修改配置文件将php.ini-development文件改为php.ini 添加...

网友评论

      本文标题:php对目录下的子目录及文件进行压缩,并解压

      本文链接:https://www.haomeiwen.com/subject/rtugwktx.html