解压缩跳过文件的文件夹 [英] Unzip file skipping folder

查看:395
本文介绍了解压缩跳过文件的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个php文件,将其从BitBucket(Git存储库)中拉出后将更新我的网站.它下载整个母版或提交的zip文件,然后将其解压缩到网站的文件夹中.

I am creating a php file that will update my site after pulling it off of BitBucket (Git repo). It downloads a zip file of the entire master or a commit, then unzips it in the website's folder.

我遇到的问题是有一个随机命名的文件夹,其中包含zip文件中的所有文件.

The problem I am having is there is a randomly named folder that contains all the files in the zip file.

我的zip文件的内容类似:

My zip file's contents is similar:

master.php
    - (bitbucketusername)-(reponame)-(commitnumber)
        - folder1
            - index.php
            - test.php
        - index.php
        - config.php
        - etc...

但是我如何绕过"随机"命名文件夹并将该文件夹的内容提取到网站的根目录?

but how can I "bypass" the "randomly" named folder and extract the contents of the folder to the website's root?

echo "Unzipping update...<br>" . PHP_EOL;
$zip = new ZipArchive;
$res = $zip->open($filename);
if ($res === TRUE) {
    $zip->extractTo('/path/to/www');
    $zip->close();
} else {
    echo 'Error: The zip file could not be opened...<br>' . PHP_EOL;
}

在这里找到一个相关问题:

Found a related question here:

在PHP中使用zip文件

但是如何使它获得随机"命名文件夹的名称?

But how can I make it get the "randomly" named folder's name?

推荐答案

将文件解压缩到tmp目录,然后将文件从随机地"命名的父文件夹中移出到所需的文件夹中.

Unzip the files to a tmp directory and then mv the files out from the "randomly" named parent folder to the folder that you want to.

echo "Unzipping update...<br>" . PHP_EOL;
$zip = new ZipArchive;
$res = $zip->open($filename);
if ($res === TRUE) {
    $zip->extractTo('/tmp/unzip');
    $directories = scandir('/tmp/unzip');
    foreach($directories as $directory){
        if($directory!='.' and $directory!='..' ){
            if(is_dir($directory)){
                  // rcopy from http://ben.lobaugh.net/blog/864/php-5-recursively-move-or-copy-files
                  rcopy('/tmp/unzip/'.$directory,'/path/to/www');
                  // rm /tmp/unzip here
            }
    }
} 

    $zip->close();
} else {
    echo 'Error: The zip file could not be opened...<br>' . PHP_EOL;
}

这篇关于解压缩跳过文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆