从结构化的文件和文件夹阵列创建ZIP / RAR / GZ存档

时间:2012-12-19 16:00:30

标签: php recursion zip rar gz

我正在尝试创建一个备份类,其主要目标是自定义类调用,以便根据用户选择仅对特定扩展,特定文件大小或主目录中的特定目录过滤最终备份

目前,该类缺乏压缩算法,因为我一直坚持寻找有关.zip压缩的解决方案。

这是一个文件和文件夹结构的示例,该类将用于创建压缩存档(zip,rar,gz等):

Array
(
    [0] => pclzip.lib.php
    [1] => index.php
    [style] => Array
        (
            [js] => Array
                (
                    [0] => jNice.js
                    [1] => jquery.js
                )

            [img] => Array
                (
                    [0] => input-shaddow-hover.gif
                    [1] => btn_right.gif
                    [2] => top-menu-bg.gif
                    [3] => top-menu-item-bg.gif
                    [4] => left-menu-bg.gif
                    [5] => button-submit.gif
                    [6] => btn_left.gif
                    [7] => select_right.gif
                    [8] => select_left.gif
                    [9] => transdmin-light.png
                    [10] => content.gif
                    [11] => input-shaddow.gif
                )

            [css] => Array
                (
                    [0] => transdmin.css
                    [1] => layout.css
                    [2] => ie7.css
                    [3] => hack.css
                    [4] => jNice.css
                    [5] => ie6.css
                    [6] => reset.css
                )

        )

    [2] => config.php
    [3] => delete.php
    [4] => restore.php
    [5] => cron.php
    [6] => manage.php
)

正如您所看到的,文件和文件夹结构需要在备份文件中保留,因此要备份的文件和文件夹列表已经过结构化,这样如果一个数组叶子包含一个子数组,那么叶子就是一个目录,如果一个叶子不包含子数组,那么叶子就是一个文件。

这就是我为创建一个递归zip文件和文件夹的过程而来的,这些文件和文件夹(应该)适用于我之前提供的结构化数组:

private function zipFileAndFolderStructureArray ( $backupContentsArray, $zipDestination, $backupRootDirectory ) {
    if ( $this -> zipObject == null ) {
        if ( extension_loaded ( 'zip' ) === true ) {
            $this -> zipObject = new ZipArchive();
            if ( ( $zipErrorCode = $this -> zipObject -> open ( $zipDestination, ZipArchive::CREATE ) ) !== true ) $this -> zipObject = null;
            else $this -> zipFileAndFolderStructureArray ( $backupContentsArray, $zipDestination, $backupRootDirectory );
        }
    }
    else if ( $this -> zipObject != null ) {
        foreach ( $backupContentsArray as $folder => $file_or_folder_list ) {
            $cwd = rtrim ( $backupRootDirectory, '/' ) . '/' . $folder . '/';
            if ( is_array ( $file_or_folder_list ) && is_dir ( $cwd . $folder ) ) {
                echo 'adding folder ' . $folder . ' in cwd ' . $cwd . '<br>';
                $this -> zipObject -> addEmptyDir ( $folder );
                $this -> zipFileAndFolderStructureArray ( $file_or_folder_list, $zipDestination, $cwd );
            }
            else if ( is_file ( $cwd . $file_or_folder_list ) ) {
                echo 'adding file ' . $file_or_folder_list . '<br>';
                $this -> zipObject -> addFromString ( $cwd . $file_or_folder_list );
            }
        }

        $this -> zipObject -> close ();
        return true;
    }
}
问题是,我在这一点上陷入困​​境。输入第一个文件夹叶后,存在此递归函数,创建一个只有1个空文件夹的存档。

你能帮我弄清楚出了什么问题吗?

我可以用来将压缩功能扩展到RAR和GZ的可能类的提示,尽可能多地重用与ZIP压缩算法相同的压缩算法?

提前谢谢

P.S添加了创建文件夹和目录结构的当前函数

public function directory_list ( $directory_base_path, $filter_dir = false, $filter_files = false, $exclude_empty_dirs = true, $include_extensions = null, $exclude_extensions = null, $exclude_files = null, $recursive = true ) {
    $directory_base_path = rtrim ($directory_base_path, "/" ) . "/";
    if ( ! is_dir ( $directory_base_path ) ) return false;

    $result_list = array();
    if ( ! is_array ( $exclude_files ) ) $exclude_array = Array ( '.', '..' );
    else $exclude_array = array_merge ( Array ( '.', '..' ), $exclude_files );

    if ( ! $folder_handle = opendir ( $directory_base_path ) ) return false;
    else{
        while ( false !== ( $filename = readdir ( $folder_handle ) ) ) {
            if ( ! in_array ( $filename, $exclude_array ) ) {
                if ( is_dir ( $directory_base_path . $filename . "/" ) ) {
                    if ( $recursive && strcmp ( $filename, "." ) != 0 && strcmp ( $filename, ".." ) != 0 ) { // prevent infinite recursion
                        $result = self::directory_list("$directory_base_path$filename/", $filter_dir, $filter_files, $exclude_empty_dirs, $include_extensions, $exclude_extensions, $exclude_files, $recursive);
                        if ( $exclude_empty_dirs ) {
                            if ( count ( array_keys ( $result ) ) > 0 ) $result_list[$filename] = $result;
                        }
                        else $result_list[$filename] = $result;
                    }
                    else if ( ! $filter_dir ) $result_list[] = $filename;
                }
                else if ( ! $filter_files ) {
                    $extension = end ( explode ( '.', $filename ) );
                    if ( ! is_array ( $include_extensions ) && count ( $include_extensions ) == 0 && ! is_array ( $exclude_extensions ) && count ( $exclude_extensions ) == 0 ) if ( $filename != '.' && $filename != '..' ) $result_list[] = $filename;
                    if ( is_array ( $exclude_extensions ) && count ( $exclude_extensions ) > 0 && ! in_array ( $extension, $exclude_extensions ) ) $result_list[] = $filename;
                    else if ( is_array ( $include_extensions ) && count ( $include_extensions ) > 0 && strlen ( $extension ) > 0 && in_array ( $extension, $include_extensions ) ) $result_list[] = $filename;
                }
            }
        }
        closedir($folder_handle);
        return $result_list;
    }
}

1 个答案:

答案 0 :(得分:1)

在对递归zip算法中的几乎所有内容进行调查和回应之后,发现文件夹结构很难处理,并且需要在路径中修复大量的斜杠。

这是同一个zipFileAndFolderStructureArray函数的固定版本

private function zipFileAndFolderStructureArray ( $backupContentsArray, $zipDestination, $backupRootDirectory, $currentWorkingDirectory = '' ) {
    if ( $this -> zipObject == null ) {
        if ( extension_loaded ( 'zip' ) === true ) {
            $this -> zipObject = new ZipArchive();
            if ( ( $zipErrorCode = $this -> zipObject -> open ( $zipDestination, ZipArchive::CREATE ) ) !== true ) $this -> zipObject = '#ERROR#';
            else $this -> zipFileAndFolderStructureArray ( $backupContentsArray, $zipDestination, $backupRootDirectory );
        }
    }
    else if ( $this -> zipObject == '#ERROR#' ) return false; // return false in case the zipArchive::open returned false on archive opening
    else {
        foreach ( $backupContentsArray as $folder => $file_or_folder_list ) {
            if ( is_array ( $file_or_folder_list ) ) {
                $current_working_folder = rtrim ( $currentWorkingDirectory, '/' ) . '/' . $folder . '/';
                if ( is_dir ( $backupRootDirectory . $current_working_folder ) ) {
                    // not necessary to add an empty folder in this case: the addFile will take care of creating the folder structure in the zip file
                    $this -> zipFileAndFolderStructureArray ( $file_or_folder_list, $zipDestination, $backupRootDirectory, rtrim ( $current_working_folder, '/' ) );
                }
            }
            else if ( is_file ( $backupRootDirectory . trim ( $currentWorkingDirectory, '/' ) . '/' . $file_or_folder_list ) ) {
                $file_insertion = $backupRootDirectory . trim ( $currentWorkingDirectory, '/' ) . '/' . $file_or_folder_list;
                $zip_filename = str_replace ( $backupRootDirectory, '', $file_insertion );
                if ( substr_count ( $zip_filename, '/' ) == 1 ) $zip_filename = trim ( $zip_filename, '/' ); // file in backup root directory fix
                $this -> zipObject -> addFile ( $file_insertion, $zip_filename );
            }
        }
        return true;
    }
}