Welcome Guest, Not a member yet? Register   Sign In
help with ZIP library
#1

[eluser]Skuja[/eluser]
How can i read directory inside other directory than root?
for example:
Code:
$this->zip->add_dir('my_project');
            $this->zip->read_dir($this->data_path.'/');
            $this->zip->archive($this->tmp_dir.'.zip');
i need to read data_path directory inside my_project.
#2

[eluser]Skuja[/eluser]
Actually i had to figure out 2 problems.
First was mentioned above. Second is that i need to compress only those files and folders inside $this->data_path not the whole path.
I extended Zip library and with a dirty hack made some changes to it:
Code:
<?php
class MY_Zip extends CI_Zip {

    var $root_path;
    var $zip_start_path;

    function My_Zip()
    {
        parent::CI_Zip();
    }

    function read_dir_contents($path,$zip_root_path="") {
        $this->root_path = str_replace("\\", "/", $path);
        $this->zip_start_path = str_replace("\\", "/", $zip_root_path);
        $this->read_dir($path);
    }
    
    function my_read_dir($path)
    {        
        if ($fp = @opendir($path))
        {            
            while (FALSE !== ($file = readdir($fp)))
            {
                if (@is_dir($path.$file) && substr($file, 0, 1) != '.')
                {
                    $this->my_read_dir($path.$file."/");
                }
                elseif (substr($file, 0, 1) != ".")
                {
                    if (FALSE !== ($data = file_get_contents($path.$file)))
                    {
                        $only_dir_conent_path = str_replace($this->root_path, "", $path);
                        $only_dir_conent_path = $this->zip_start_path.str_replace("\\", "/", $only_dir_conent_path);
                        $this->add_data($only_dir_conent_path.$file, $data);
                    }
                }
            }
            return TRUE;
        }
    }
}
?>

One weird thing is that it compresses only those folders which contains files. Why?
And now i need to solve a new problem: How to delete zip folder after download() method is called - i believe it cant be done because of exit() in force_download() helper function. Any Ideas/Suggestions?
#3

[eluser]Unknown[/eluser]
The reason why the zip helper only compresses non-empty folders is because of the check "substr($file, 0, 1) != '.'" which has another side effect on linux, which is not to add hidden files to the archive.
I would suggest the maintainer of the online documentation to add a few lines specifying the exact behavior of the read_dir function, since as it stands it is misleading.




Theme © iAndrew 2016 - Forum software by © MyBB