在PHP中提取ZIP文件的子文件夹

在PHP中提取ZIP文件的子文件夹

本文介绍了在PHP中提取ZIP文件的子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php脚本解压缩ZIP文件.但是此脚本仅解压缩一级目录而不解压缩该文件的子目录脚本:

I am using a php script to unzip ZIP file. but this script unzip only one level of directories without extracting the sub directories of that filethe script:

$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

例如:如果test.zip包含2个文件夹:folder1 \ file.png,folder2 \ folder3 \ file3.png

for example: if the test.zip contains 2 folders: folder1\file.png, folder2\folder3\file3.png

解压缩此ZIP文件后,我只看到folder1 *.*和folder2 *.*,但没有子目录folder3.

after extracting this ZIP file, i only see the folder1*.* and folder2*.* but without the sub directory folder3.

我该如何改善?

推荐答案

我认为此PHP手册将对您有所帮助 http://php.net/manual/en/ref.zip.php

I think this PHP manual will be helpful to youhttp://php.net/manual/en/ref.zip.php

<?php
$file = "2537c61ef7f47fc3ae919da08bcc1911.zip";
$dir = getcwd();
function Unzip($dir, $file, $destiny="")
{
    $dir .= DIRECTORY_SEPARATOR;
    $path_file = $dir . $file;
    $zip = zip_open($path_file);
    $_tmp = array();
    $count=0;
    if ($zip)
    {
        while ($zip_entry = zip_read($zip))
        {
            $_tmp[$count]["filename"] = zip_entry_name($zip_entry);
            $_tmp[$count]["stored_filename"] = zip_entry_name($zip_entry);
            $_tmp[$count]["size"] = zip_entry_filesize($zip_entry);
            $_tmp[$count]["compressed_size"] = zip_entry_compressedsize($zip_entry);
            $_tmp[$count]["mtime"] = "";
            $_tmp[$count]["comment"] = "";
            $_tmp[$count]["folder"] = dirname(zip_entry_name($zip_entry));
            $_tmp[$count]["index"] = $count;
            $_tmp[$count]["status"] = "ok";
            $_tmp[$count]["method"] = zip_entry_compressionmethod($zip_entry);

            if (zip_entry_open($zip, $zip_entry, "r"))
            {
                $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                if($destiny)
                {
                    $path_file = str_replace("/",DIRECTORY_SEPARATOR, $destiny . zip_entry_name($zip_entry));
                }
                else
                {
                    $path_file = str_replace("/",DIRECTORY_SEPARATOR, $dir . zip_entry_name($zip_entry));
                }
                $new_dir = dirname($path_file);

                // Create Recursive Directory (if not exist)
                if (!file_exists($new_dir)) {
                  mkdir($new_dir, 0700);
                }

                $fp = fopen($dir . zip_entry_name($zip_entry), "w");
                fwrite($fp, $buf);
                fclose($fp);

                zip_entry_close($zip_entry);
            }
            echo "\n</pre>";
            $count++;
        }

        zip_close($zip);
    }
}
Unzip($dir,$file);
?>

这篇关于在PHP中提取ZIP文件的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:05