本文介绍了Codeigniter - 将多个路由压缩到一个具有新目录结构的文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将服务器上两个目录的内容合并成一个新的zip文件。
I'm looking to combine the contents of two directories on my server into one new zip file.
示例:
将/ games / wheel / *和/ games / SDK / com / *的内容合并到新zip文件的根目录中。
Example:Combine the contents of /games/wheel/* and /games/SDK/com/* into the root of a new zip file.
现有目录结构:
- games
- SDK
- com
- folder1
- file1
- file1
- wheel
- game_file1
- game_file2
新目录结构(在您解压缩新文件后):
- folder1
- file1
- file2
- game_file1
- game_file2
b $ b
使用codeigniter的当前Zip库,如何将现有的文件结构合并成一个新的并压缩呢?
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?
推荐答案
MY_Zip.php - 扩展Codeigniter的Zip库
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);
}
}
strong>
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
这篇关于Codeigniter - 将多个路由压缩到一个具有新目录结构的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!