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

Php 从文件和文件夹的结构化阵列创建ZIP/RAR/GZ存档,php,recursion,zip,rar,gzip,Php,Recursion,Zip,Rar,Gzip,我正在尝试创建一个备份类,主要目标是自定义类调用,以便根据用户选择,仅在主目录中的特定扩展名、特定文件大小或特定目录上筛选最终备份 目前,这个类缺少压缩算法,因为我一直在寻找关于.zip压缩的解决方案 下面是一个文件和文件夹结构的示例,该类将使用该文件和文件夹结构创建压缩存档(zip、rar、gz等): 如您所见,文件和文件夹结构需要保存在备份文件中,因此要备份的文件和文件夹列表的结构是这样的:如果数组叶包含子数组,则叶为目录;如果叶不包含子数组,则叶为文件 以下是我为递归压缩文件和文件夹创建过

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

目前,这个类缺少压缩算法,因为我一直在寻找关于.zip压缩的解决方案

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

如您所见,文件和文件夹结构需要保存在备份文件中,因此要备份的文件和文件夹列表的结构是这样的:如果数组叶包含子数组,则叶为目录;如果叶不包含子数组,则叶为文件

以下是我为递归压缩文件和文件夹创建过程的目的,这些文件和文件夹(应该)适用于我之前介绍的结构化阵列:

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;
    }
}

在对递归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;
    }
}

您在这里使用的PHP版本是什么?这听起来像是一份适合年轻人的工作。这是一个5.3版本,尽管手册错误地说它是5.0版本。目前运行在5.3.3版本上,我看到了许多使用RecursiveDirectoryIterator的目录结构创建者的示例,但我希望保持与旧版本的PHP兼容,因为一旦完成,我想向公众共享这个类。因此,我提出了一个不同的目录结构创建者使用一个“旧的方法”程序(我在上面的问题中添加了),因为PHP维护者在5.3之前已经放弃了对所有东西的支持,而5.5即将到来,不考虑旧版本可能是一件好事。即使是以一些人无法运行它为代价。是的,我最近读到了,即使我继续认为我应该为大多数配置开发它。我愿意重写这些过程,并使它们基于递归DirectoryIterator。我想的是保持“目录结构创建者”独立于压缩算法,这样如果我想在将来添加更多的压缩算法,我就不必复制和粘贴我已经为其他压缩算法编写的代码。你能指导我如何完成这样一件事吗,专注于压缩算法本身?
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;
    }
}
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;
    }
}