Codeigniter - 使用新的目录结构将多个目标压缩到一个文件中

zec*_*hdc 0 php codeigniter

我希望将我服务器上两个目录的内容组合成一个新的zip文件.

示例: 将/ games/wheel/*和/ games/SDK/com/*的内容组合到新zip文件的根目录中.

现有目录结构:

- games
    - SDK
      - com
        - folder1
           - file1
           - file1
    - wheel
      - game_file1
      - game_file2
Run Code Online (Sandbox Code Playgroud)

新目录结构(解压缩新文件后):

- folder1
   - file1
   - file2
- game_file1
- game_file2
Run Code Online (Sandbox Code Playgroud)

使用codeigniter的当前Zip库,如何将现有的文件结构组合成一个新的文件结构并压缩它?有没有人扩展它这样做?

zec*_*hdc 5

MY_Zip.php - 扩展Codeigniter的Zip库

<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class MY_Zip extends CI_Zip 
{

/**
 * Read a directory and add it to the zip using the new filepath set.
 *
 * This function recursively reads a folder and everything it contains (including
 * sub-folders) and creates a zip based on it.  You must specify the new directory structure.
 * The original structure is thrown out.
 *
 * @access  public
 * @param   string  path to source
 * @param   string  new directory structure
 */
function get_files_from_folder($directory, $put_into) 
{
    if ($handle = opendir($directory)) 
    {
        while (false !== ($file = readdir($handle))) 
        {
            if (is_file($directory.$file)) 
            {
                $fileContents = file_get_contents($directory.$file);

                $this->add_data($put_into.$file, $fileContents);

            } elseif ($file != '.' and $file != '..' and is_dir($directory.$file)) {

                $this->add_dir($put_into.$file.'/');

                $this->get_files_from_folder($directory.$file.'/', $put_into.$file.'/');
            }

        }//end while

    }//end if

    closedir($handle);
}

}
Run Code Online (Sandbox Code Playgroud)

用法:

$folder_in_zip = "/"; //root directory of the new zip file

$path = 'games/SDK/com/';
$this->zip->get_files_from_folder($path, $folder_in_zip);

$path = 'games/wheel/';
$this->zip->get_files_from_folder($path, $folder_in_zip);

$this->zip->download('my_backup.zip');
Run Code Online (Sandbox Code Playgroud)

结果:

mybackup.zip/
  - folder1
    - file1
    - file2
  - game_file1
  - game_file2
Run Code Online (Sandbox Code Playgroud)