CodeIgniter Forums
Zip class: don't store rootfolder in archive but DO store subfolders - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Zip class: don't store rootfolder in archive but DO store subfolders (/thread-5722.html)



Zip class: don't store rootfolder in archive but DO store subfolders - El Forum - 02-01-2008

[eluser]codex[/eluser]
When using the Zip encoding class you have to point to a directory that holds the files to be archived. But when you zip the files the root folder is zipped with it also, which I don't want.

I have this piece of code, provided by forum member Tassoman (http://ellislab.com/forums/viewthread/46681/). It pretty much does what I need, but it also strips the subfolders and are therefore not included. I must admit that I don't fully understand what's happening in the code and how/if I can fix it. Anyone got a clue?

Code:
class MY_Zip extends CI_Zip {

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

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



Zip class: don't store rootfolder in archive but DO store subfolders - El Forum - 09-16-2008

[eluser]wessley[/eluser]
I'm having the same problem.

Using:

Code:
$this->zip->read_dir('/path/to/my/directory/');

Makes a zip file with the following directory structure:

Code:
-path
  -to
    -my
      -directory
        ... stuff in the directory ...

Which is pretty annoying.

Anyone?


Zip class: don't store rootfolder in archive but DO store subfolders - El Forum - 06-12-2010

[eluser]ocergyNohtna[/eluser]
wow, this thread is from 08.... does anyone know how to remove such a structure? i think we all want the files in the deepest folder to be added at the root of the zip archive. is there anyway to do this without hacking the zip class directly?

heheh, so it's not exactly hacking the zip class... hell it's only one line. i guess i'll rewrite in a config option for the feature.

so actually it's as easy as this:

Code:
function read_dir($path, $folders = FALSE)
    {    
        if ($fp = @opendir($path))
        {
            while (FALSE !== ($file = readdir($fp)))
            {
                if (@is_dir($path.$file) && substr($file, 0, 1) != '.')
                {                    
                    $this->read_dir($path.$file."/");
                }
                elseif (substr($file, 0, 1) != ".")
                {
                    if (FALSE !== ($data = file_get_contents($path.$file)))
                    {    
                        if($folders === TRUE) {
                            $this->add_data(str_replace("\\", "/", $path).$file, $data);
                        } else {
                            $this->add_data($file, $data);
                        }
                    }
                }
            }
            return TRUE;
        }
    }

i just added a new param to pass to read_dir if needed. depending on your typical usage you may would flip it the other way to always put the subfolders, but that's just me. hope this helps someone. Tongue


Zip class: don't store rootfolder in archive but DO store subfolders - El Forum - 06-14-2010

[eluser]wessley[/eluser]
Thanks for that ocergyNohtna... I can't even remember what do I needed this ZIP thing in the first place... LOL

I'll bookmark this thread in case I need this sometime.

Thanks again!