tags:

views:

111

answers:

1

see this http://stackoverflow.com/questions/2190540/adds-an-empty-directory-in-the-archive i cant use that code

function addFolderToZip($dir, $zipArchive, $zipdir = ''){
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {

        //Add the directory
        $zipArchive->addEmptyDir($dir);

        // Loop through all the files
        while (($file = readdir($dh)) !== false) {

            //If it's a folder, run the function again!
            if(!is_file($dir . $file)){
                // Skip parent and root directories
                if( ($file !== ".") && ($file !== "..")){
                    addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file . "/");
                }

            }else{
                // Add the files
                $zipArchive->addFile($dir . $file, $zipdir . $file);

            }
        }
    }

please write example for me

the second problem is tooooo complex when i using addfile function its will add and appear in the archive as a file and this great now when i using

$z = new ZipArchive();
$z->open('test.zip')

for ($i=0; $i< $z->numFiles;$i++) {
 $aZipDtls = $z->statIndex($i);

echo $aZipDtls['name'];
}

its now show if i add a file in folder like that

    $zip->addFile('/path/to/index.txt', 'dir/newname.txt');

it show in the winrar soft a dir then a file but in the code its show it as one file file like that in winrar

dir/

dir/newname.txt

in my php system just only show one file without its dir like that

dir/newname.txt

this mean its imposible to add a new file in a dir

+1  A: 

Difficult to know what you want, but here goes:

<?php
$zip = new ZipArchive();
$zip->open('test.zip');
$zip->addFile('/path/to/newname.txt','dir/newname1.txt');
$zip->addFile('/path/to/newname.txt','dir/newname2.txt');
$zip->addFile('/path/to/newname.txt','dir/dir/newname3.txt');
$zip->addFile('/path/to/newname.txt','dir/dir/dir/newname4.txt');

for ($i=0; $i< $zip->numFiles;++$i) {
    $aZipDtls = $zip->statIndex($i);
    echo $aZipDtls['name'],"\n";
}

$zip->close();
?>

Should cover all questions. That will unzip with exactly the structure you'd expect it to. The discrepancy is likely due to the way WinRar displays the archive structure.

Mike
I wonder why this wasn't accepted or even commented on, by @moustafa
Cawas