CodeIgniter Forums
ZIP a specific directory. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: ZIP a specific directory. (/showthread.php?tid=13420)

Pages: 1 2 3


ZIP a specific directory. - El Forum - 11-21-2008

[eluser]Lazos[/eluser]
Hi is there any way to ZIP a specific directory?

The read_dir will compress all the folders and subfolders from the root to the destination folder.


ZIP a specific directory. - El Forum - 02-24-2009

[eluser]leighmarble[/eluser]
+1

Doesn't seem like desirable behavior.

This issue goes back aways: http://ellislab.com/forums/viewthread/70483/

It may or may not constitute a bug, since the docs imply that this might be the intended behavior:

Quote:All files contained within the supplied path will be encoded, as will any sub-folders contained within it.

In any case, I'm wondering if this has been changed in more recent CI beta builds... I've been sticking with 1.7.1 for now.

cheers,
Leigh


ZIP a specific directory. - El Forum - 02-24-2009

[eluser]leighmarble[/eluser]
I hacked at a solution for this today, but ended up with a new problem...

This code requires PHP 5. Save it as MY_Zip.php, into application/libraries:


Code:
class MY_Zip extends CI_Zip {

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


    function read_dir($path)
    {
        $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
        foreach($objects as $name => $object)
        {

            //echo $objects->getDepth() .": $name<br />";  // uncomment for troubleshooting

            if (FALSE !== ($data = file_get_contents($name)) && $objects->getDepth() >=0 )  // prevent moving up to parent directories
            {
                $this->add_data($name, $data);
            }
        }

        return TRUE;
    }
}


When I uncomment the "echo" line above, the readout looks good.

Then, I enable the $this->zip->download in my controller, and give the download a try... a ZIP file shows up, but upon unzipping, it has this extension:

.zip.cpgz

This file, in turn, "unzips" to a zip, and back and forth and so on.

Any ideas?

thanks,
Leigh


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]leighmarble[/eluser]
Arg... anyone?


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]TheFuzzy0ne[/eluser]
Sorry, but I don't understand the problem. Can you explain exactly what you're trying to do, what you're expecting to happen, and what is happening instead?


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]leighmarble[/eluser]
[quote author="TheFuzzy0ne" date="1236138411"]Sorry, but I don't understand the problem. Can you explain exactly what you're trying to do, what you're expecting to happen, and what is happening instead?[/quote]

Sure thing, sorry.

Say I load the Zip Encoding class, and run its read_dir and download methods:

Code:
$path = '/files/mydirectory';

$this->zip->read_dir($path);

$this->zip->download('mydirectory.zip');

When I unzip mydirectory.zip, what shows up on my desktop is a folder named "files", not a folder named "mydirectory".

The read_dir method is doing a recursive read, and getting all the subfolders of "files". Problem is, it is also traversing UP the directory structure, and getting all the parent folders of "files" as well.

Thank you,
Leigh


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]TheFuzzy0ne[/eluser]
Try adding a trailing slash to the end of the path if it's a directory.


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]leighmarble[/eluser]
[quote author="TheFuzzy0ne" date="1236139651"]Try adding a trailing slash to the end of the path if it's a directory.[/quote]

Thank you - but I tried that, and it doesn't make a difference.

The example code in my previous post was hastily copied/edited from the documention, not from my code.

My code actually looks like this:

Code:
$path = 'files/mydirectory/';
$this->zip->read_dir($path);
$this->zip->download('mydirectory.zip');

So, I *do* have the trailing slash, and I *don't* have the leading slash. (Including the leading slash causes a blank page to load, and no download happens. The "files" directory is on the same level as the index.php front controller, so it finds "files" just fine without a leading slash.)

Thanks again,
Leigh


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]TheFuzzy0ne[/eluser]
How about if you use the absolute path rather than the relative path?


ZIP a specific directory. - El Forum - 03-03-2009

[eluser]leighmarble[/eluser]
[quote author="TheFuzzy0ne" date="1236145174"]How about if you use the absolute path rather than the relative path?[/quote]

Like this?

Code:
$path = base_url().'files/mydirectory/';

That produces the same result as including the leading slash - a blank browser page, and no download.