Solved

ZipArchive(): add a whole directory recursively, keeping the same structure

Posted on 2008-10-28
8
1,871 Views
Last Modified: 2013-12-13
I want to add a whole directory recursively, keeping the same structure.

How can I do this?  I found a class but I don't know how to use it.

I want to backup EVERYTHING in:
       "C:\\Server\\root\\example.com"

And place in in a zip:
       "C:\\Temp\\example.com.zip"

<?php

class MyZipArchive extends ZipArchive

{

    /**

     *

     * Adds a directory recursively.

     *

     * @param string $filename The path to the file to add.

     *

     * @param string $localname Local name inside ZIP archive.

     *

     */

    public function addDir($filename, $localname)

    {

        $this->addEmptyDir($localname);

        $iter = new RecursiveDirectoryIterator($filename);
 

        foreach ($iter as $fileinfo) {

            if (! $fileinfo->isFile() && !$fileinfo->isDir()) {

                continue;

            }
 

            $method = $fileinfo->isFile() ? 'addFile' : 'addDir';

            $this->$method($fileinfo->getPathname(), $localname . '/' .

                $fileinfo->getFilename());

        }

    }

}

?>

Open in new window

0
Comment
Question by:hankknight
  • 4
  • 2
  • 2
8 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22821805
>>extends ZipArchive
you will need the ZipArchive class as well.

try:
$myArchive = new MyZipArchive();
$myArchive.addDir("C:\\Server\\root\\example.com","C:\\Temp\\example.com.zip");
0
 
LVL 16

Author Comment

by:hankknight
ID: 22822055
I get an error:

Call to undefined function addDir()
<?php
 

// header('Content-type: application/tar');
 
 

$zip = new ZipArchive();

$filename = "c:\\Temp\\wb.zip";
 

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {

    exit("Cannot open <$filename>\n");

}
 

$zip.addDir("C:\\Server\\root\\example.com");
 

echo "numfiles: " . $zip->numFiles . "\n";

echo "status:" . $zip->status . "\n";

$zip->close();
 

class MyZipArchive extends ZipArchive

{

    /**

     *

     * Adds a directory recursively.

     *

     * @param string $filename The path to the file to add.

     *

     * @param string $localname Local name inside ZIP archive.

     *

     */

    public function addDir($filename, $localname)

    {

        $this->addEmptyDir($localname);

        $iter = new RecursiveDirectoryIterator($filename);
 

        foreach ($iter as $fileinfo) {

            if (! $fileinfo->isFile() && !$fileinfo->isDir()) {

                continue;

            }
 

            $method = $fileinfo->isFile() ? 'addFile' : 'addDir';

            $this->$method($fileinfo->getPathname(), $localname . '/' .

                $fileinfo->getFilename());

        }

    }

}
 

?> 

Open in new window

0
 
LVL 3

Expert Comment

by:DarkFish
ID: 22822065
That should be $zip->addDir() not $zip.addDir :)
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 200 total points
ID: 22822082
sorry. Should have been an arrow, not a dot. try:
$myArchive = new MyZipArchive();
$myArchive->addDir("C:\\Server\\root\\example.com","C:\\Temp\\example.com.zip");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Author Comment

by:hankknight
ID: 22822168
Now I get this error:
Fatal error: Call to undefined method MyZipArchive::addEmptyDir() in C:\Server\root\example.com\do.php on line 31

Am I doing something wrong or does my server just not support this?
<?php
 

$zip = new MyZipArchive();

$filename = "c:\\Temp\\wb.zip";
 

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {

    exit("Cannot open <$filename>\n");

}
 

$zip->addDir("C:\\Server\\root\\example.com","wb");
 

echo "numfiles: " . $zip->numFiles . "\n";

echo "status:" . $zip->status . "\n";

$zip->close();
 

class MyZipArchive extends ZipArchive

{

    /**

     *

     * Adds a directory recursively.

     *

     * @param string $filename The path to the file to add.

     *

     * @param string $localname Local name inside ZIP archive.

     *

     */

    public function addDir($filename, $localname)

    {

        $this->addEmptyDir($localname);

        $iter = new RecursiveDirectoryIterator($filename);
 

        foreach ($iter as $fileinfo) {

            if (! $fileinfo->isFile() && !$fileinfo->isDir()) {

                continue;

            }
 

            $method = $fileinfo->isFile() ? 'addFile' : 'addDir';

            $this->$method($fileinfo->getPathname(), $localname . '/' .

                $fileinfo->getFilename());

        }

    }

}
 

?> 

Open in new window

0
 
LVL 3

Accepted Solution

by:
DarkFish earned 300 total points
ID: 22824156
You need to include() the file that contains the original ZipArchive class; at the moment PHP is extending a blank class, meaning that the method addEmptyDir() does not exist.

Make sure you include() the file that contains the ZipArchive class first at the top of your script, and try again. :)
0
 
LVL 82

Expert Comment

by:hielo
ID: 22824311
>>on line 31
The line giving you the problem is line 10. What else is on that file?
0
 
LVL 82

Expert Comment

by:hielo
ID: 22824752
I thought that:
"you will need the ZipArchive class as well."

was clear enough :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now