我希望将服务器上两个目录的内容合并到一个新的zip文件中。
示例: 将/ games / wheel / *和/ games / SDK / com / *的内容组合到新zip文件的根目录中。
现有目录结构:
- games
- SDK
- com
- folder1
- file1
- file1
- wheel
- game_file1
- game_file2
新目录结构(解压缩新文件后):
- folder1
- file1
- file2
- game_file1
- game_file2
使用codeigniter的当前Zip库,如何将现有文件结构合并为一个新文件并压缩?有人扩展它吗?
答案 0 :(得分:5)
<?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);
}
}
$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');
mybackup.zip/
- folder1
- file1
- file2
- game_file1
- game_file2