Zip directory with PHP

7

I have a code that compresses file and directory. The problem is that it compresses the files and directories that are in the root, but the subdirectories they are not compressed, for example I have these directories:

  

folder /, folder / file.php, folder / other / folder2, file1.php

It only compresses / folder and file1.php ...

It should compress the .php file, another /, folder2 /

Code:

<?php

function Show_files($local){

    $zip = new ZipArchive();

    if($zip->open('compact.zip', ZIPARCHIVE::CREATE) == TRUE){

    $open = opendir($local);

    while($folder = readdir($open)){

        if(is_dir($local.$folder) && $folder != '.' && $folder != '..'){

            echo $local.$folder.'<br>';

            $zip->addEmptyDir($folder);

            Show_files($local.$folder.'/');

        }elseif(is_file($local.$folder) && $folder != '.' && $folder != '..'){

            $zip->addFile($local.$folder, $folder);

                echo $local.$folder.'<br>';
        }
    }
}

}
    $raiz = str_replace("\", "/", getcwd())."/";
    Show_files($raiz);

?>
    
asked by anonymous 10.07.2014 / 03:53

1 answer

5

What is happening is that you are rewriting the file because it is a recursive function, so the instance $zip is created all the times it passes through the recursive function call. To do this, create the $zip instance and pass it as a function parameter.

Example with function :

<?php
    function Compact($zip, $cwd) {
        $open = opendir($cwd);
        while($folder = readdir($open))
        {
            if ($folder != '.' && $folder != '..'){
                if (is_dir($cwd.'/'.$folder))
                {
                    $dir = str_replace('./', '',($cwd.'/'.$folder));
                    $zip->addEmptyDir($dir);
                    Compactar($zip, $dir);
                }
                elseif (is_file($cwd.'/'.$folder))
                {
                    $arq = str_replace('./', '',$cwd.'/'.$folder);                  
                    $zip->addFile($arq);                                        
                }
            }
        }
    }

    $zip = new ZipArchive();
    if ($zip->open("arquivoFAfa.zip", ZIPARCHIVE::CREATE) === true){
        Compact($zip, ".");
    }
    $zip->close();

Example with class extending ZipArchive

<?php
    class Zipper extends ZipArchive 
    {
        public function Compact($cwd) {
            $open = opendir($cwd);
            while($folder = readdir($open))
            {
                if ($folder != '.' && $folder != '..')
                {
                    if (is_dir($cwd.'/'.$folder))
                    {
                        $dir = str_replace('./', '',($cwd.'/'.$folder));
                        $this->addEmptyDir($dir);
                        $this->Compact($dir);
                    } 
                    elseif (is_file($cwd.'/'.$folder))
                    {
                        $arq = str_replace('./', '',$cwd.'/'.$folder);                  
                        $this->addFile($arq);                   
                    }
                }
            }
        }
    }

    $zip = new Zipper();
    if ($zip->open("arquivoFAClassAbc.zip", ZIPARCHIVE::CREATE) === true){
        $zip->Compact(".");
    }
    $zip->close();

Tip: Use the second option because you will not have this instance problem, although the first one is to only create the instance out as reported at the beginning of the response.     

10.07.2014 / 06:12