Jump to content

Zipping up a folder and its contents?


seanhess

Recommended Posts

Hey All,

 

I installed the zip extension for PHP 5.2, but reading the documentation, I can't figure out how to zip up an existing folder.  I mean, I have this folder with a bunch of files in it, right?  I *could* scan the whole thing and add each file to a newly created zip archive, but I don't want to.

 

Is there any way to tell PHP to zip up a folder and have its contents be everything inside that folder?

 

Thanks

~sean

Link to comment
Share on other sites

*Bump*  Dang, this forum moves fast... I'm nearing the bottom of the page.

 

Anyone know?  Why do all PHP zipping utilities focus so much on manipulating zip archives, when the most standard process is zipping/unzipping entire archives and directories

Link to comment
Share on other sites

www.php.net/dir -> usercomments

 

dkflbk at nm dot ru

14-Sep-2006 04:29

I wrote a simple backup script which puts all files in his folder (and all of the sub-folders) in one TAR archive...

(It's classic TAR format not USTAR, so filename and path to it can't be longer then 99 chars)

<?php
/***********************************************************
*   Title:  Classic-TAR based backup script v0.0.1-dev
  **********************************************************/

Class Tar_by_Vladson {
    var $tar_file;
    var $fp;
    function Tar_by_Vladson($tar_file='backup.tar') {
        $this->tar_file = $tar_file;
        $this->fp = fopen($this->tar_file, "wb");
        $tree = $this->build_tree();
        $this->process_tree($tree);
        fputs($this->fp, pack("a512", ""));
        fclose($this->fp);
    }
    function build_tree($dir='.'){
        $handle = opendir($dir);
        while(false !== ($readdir = readdir($handle))){
            if($readdir != '.' && $readdir != '..'){
                $path = $dir.'/'.$readdir;
                if (is_file($path)) {
                    $output[] = substr($path, 2, strlen($path));
                } elseif (is_dir($path)) {
                    $output[] = substr($path, 2, strlen($path)).'/';
                    $output = array_merge($output, $this->build_tree($path));
                }
            }
        }
        closedir($handle);
        return $output;
    }
    function process_tree($tree) {
        foreach( $tree as $pathfile ) {
            if (substr($pathfile, -1, 1) == '/') {
                fputs($this->fp, $this->build_header($pathfile));
            } elseif ($pathfile != $this->tar_file) {
                $filesize = filesize($pathfile);
                $block_len = 512*ceil($filesize/512)-$filesize;
                fputs($this->fp, $this->build_header($pathfile));
                fputs($this->fp, file_get_contents($pathfile));
                fputs($this->fp, pack("a".$block_len, ""));
            }
        }
        return true;
    }
    function build_header($pathfile) {
        if ( strlen($pathfile) > 99 ) die('Error');
        $info = stat($pathfile);
        if ( is_dir($pathfile) ) $info[7] = 0;
        $header = pack("a100a8a8a8a12A12a8a1a100a255",
            $pathfile,
            sprintf("%6s ", decoct($info[2])),
            sprintf("%6s ", decoct($info[4])),
            sprintf("%6s ", decoct($info[5])),
            sprintf("%11s ",decoct($info[7])),
            sprintf("%11s", decoct($info[9])),
            sprintf("%8s", " "),
            (is_dir($pathfile) ? "5" : "0"),
            "",
            ""
            );
        clearstatcache();
        $checksum = 0;
        for ($i=0; $i<512; $i++) {
            $checksum += ord(substr($header,$i,1));
        }
        $checksum_data = pack(
            "a8", sprintf("%6s ", decoct($checksum))
            );
        for ($i=0, $j=148; $i<7; $i++, $j++)
            $header[$j] = $checksum_data[$i];
        return $header;
    }
}

header('Content-type: text/plain');
$start_time = array_sum(explode(chr(32), microtime()));
$tar = & new Tar_by_Vladson();
$finish_time = array_sum(explode(chr(32), microtime()));
printf("The time taken: %f seconds", ($finish_time - $start_time));
?>

 

It is for TAR, but should be similiar for ZIP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.