开发者

Codeigniter - Zip multiple directries into one file with new directory structure

开发者 https://www.devze.com 2023-03-19 13:35 出处:网络
I\'m looking to combine the contents of two directories on my server into one new zip file. Example: Combine the contents of /games/wheel/* and /games/SDK/com/* into the root of a new zip file.

I'm looking to combine the contents of two directories on my server into one new zip file.

Example: Combine the contents of /games/wheel/* and /games/SDK/com/* into the root of a new zip file.

Existing Directory Structure:

- games
    - SDK
      - com
        - folder1
           - file1
           - file1
    - wheel
      - game_file1
      - game_file2

New Directory Structure (After you unzip the new file):

- folder1
   - file1
   - file2
- game_file1
- game_file2

Using codeigniter's current Zip library, how do I combine the existing file structure into a new one and zip it? Has anyone extend开发者_开发问答ed it to do this?


MY_Zip.php -- Extend Codeigniter's Zip Library

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

}

Usage:

$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');

Result:

mybackup.zip/
  - folder1
    - file1
    - file2
  - game_file1
  - game_file2
0

精彩评论

暂无评论...
验证码 换一张
取 消