duanpai9945 2011-07-12 17:42
浏览 31
已采纳

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

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 extended it to do this?

  • 写回答

1条回答 默认 最新

  • duanjizi9443 2011-07-13 02:23
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?