Solved

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

Posted on 2008-10-28
8
1,865 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
That should be $zip->addDir() not $zip.addDir :)
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 200 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Author Comment

by:hankknight
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
I thought that:
"you will need the ZipArchive class as well."

was clear enough :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 26
Multilanguage Database Design in MySQL 5 34
Adding Extra Information box 4 24
XML Data Missing in PHP SimpleXML 8 21
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

16 Experts available now in Live!

Get 1:1 Help Now