首页 > 编程语言 >php 解压zip 格式的文件

php 解压zip 格式的文件

时间:2022-11-04 18:57:27浏览次数:42  
标签:解压 name zip file path directory entry php

<?php

/**
 *  function: 解压zip 格式的文件
 */
class Unzip
{

    public function __construct()
    {
        //init code here...
        header("content-type:text/html;charset=utf8");
    }

    /**
     * 解压文件到指定目录
     *
     * @param string   zip压缩文件的路径
     * @param string   解压文件的目的路径
     * @param boolean  是否以压缩文件的名字创建目标文件夹
     * @param boolean  是否重写已经存在的文件
     *
     * @return  boolean  返回成功 或失败
     */
    public function unzip($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true)
    {
        $file_name_arr = array();
        $zip = zip_open($src_file);
        if (is_resource($zip)) {
            $splitter = ($create_zip_name_dir === true) ? "." : "/";
            if ($dest_dir === false) {
                $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . "/";
            }

            // 如果不存在 创建目标解压目录
            $this->create_dirs($dest_dir);

            // 对每个文件进行解压
            while ($zip_entry = zip_read($zip)) {
                // 文件不在根目录
                $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
                if ($pos_last_slash !== false) {
                    // 创建目录 在末尾带 /
                    $this->create_dirs($dest_dir . substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1));
                }

                // 打开包
                if (zip_entry_open($zip, $zip_entry, "r")) {

                    // 文件名保存在磁盘上
                    $file_name = $dest_dir . zip_entry_name($zip_entry);

                    // 检查文件是否需要重写
                    if ($overwrite === true || $overwrite === false && !is_file($file_name)) {
                        // 读取压缩文件的内容
                        $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

                        @file_put_contents($file_name, $fstream);
                        // 设置权限
                        chmod($file_name, 0777);
                        //echo "save: " . $file_name . "<br />";
                        $file_name_arr[] = $file_name;
                    }
                    // 关闭入口
                    zip_entry_close($zip_entry);
                }
            }
            // 关闭压缩包
            zip_close($zip);
        }
        return $file_name_arr;
    }

    /**
     * 创建目录
     */
    public function create_dirs($path)
    {
        if (!is_dir($path)) {
            $directory_path = "";
            $directories = explode("/", $path);
            array_pop($directories);

            foreach ($directories as $directory) {
                $directory_path .= $directory . "/";
                if (!is_dir($directory_path)) {
                    mkdir($directory_path);
                    chmod($directory_path, 0777);
                }
            }
        }
    }

}


<?php
require_once "./unzip.php";

$dest_dir = "D:\\reloadD\\www\\php\\study\\unzip\\";

$dst_zip = $dest_dir . "pdfbox_examples-master.zip";


$z = new Unzip();
$z->create_dirs($dest_dir);

$file_name = $z->unzip($dst_zip, $dest_dir, true, false);

echo "<pre>";
var_dump($file_name);

标签:解压,name,zip,file,path,directory,entry,php
From: https://www.cnblogs.com/fuqian/p/16858771.html

相关文章

  • PHP WEB怎么实现大文件上传
    ​PHP用超级全局变量数组$_FILES来记录文件上传相关信息的。1.file_uploads=on/off 是否允许通过http方式上传文件2.max_execution_time=30 允许脚本最大执行时间......
  • C# HttpClient请求gzip
    //设置HttpClientHandler的AutomaticDecompressionvarhandler=newHttpClientHandler(){AutomaticDecompression=DecompressionMethods.GZip};//创建HttpClient(......
  • php注解使用示例
    今天看到php注解的介绍文章很感兴趣,动手实际试了试挺好玩,写这篇文章记录下php从8开始支持原生注解功能了,我们可以写个小的例子看看注解怎么玩。 先确定我们的任务目标......
  • linux 文件打包 / 分割 / 组合 / 解压
    较多时候我们在linux下使用tar打包文件夹,会出现打包后的文件过大,在文件copy过程中需要切片成较小文件。步骤:1.先压缩,然后切片**打包命令:#tarzcvfimages.tar.......
  • php curl请求 header头携带参数
    phpcurl请求header头携带参数  ......
  • 十堰网站建设用php、java、.net哪个编程语言好
    前言对于这个问题,我的回答是:在十堰网站建设时没有什么编程语言是最好的,只有适合自己的才是最好的!或许你对我的回复不是很满意,不过没关系,你可以看看我的分析就知道了。Php......
  • [2021N1CTF国际赛]Easyphp-Wp
    文章目录​​写在前面​​​​Wp​​​​简单分析​​​​小实验验证猜想可效性​​​​构造tar​​​​参考文章​​写在前面在各种带飞的情况下,web被带ak了,这里比较想写一......
  • 浅谈PHP设计模式的适配器模式
    简介:适配器模式属于结构型设计模式。将一个类的接口转换成可应用的兼容接口。适配器使原本由于接口不兼容而不能一起工作的那些类可以一起工作。适配器模式有两种实现方......
  • 坐标经纬度 - PHP 工具类
    坐标经纬度-PHP工具类<?phpnamespaceApp\common\Helpers\tools;classCoordinateTransformHelper{privatestatic$x_pi=3.14159265358979324*3000.0/......
  • 解压缩命令
    其他解压缩命令:1、.tar用tar–xvf解压2、.gz用gzip-d或者gunzip解压3、.tar.gz和.tgz用tar–xzf解压4、.bz2用bzip2-d或者用bunzip2解压5、.tar.bz2......