Results 1 to 5 of 5

Thread: zip class

  1. #1

    zip class

    This script is not working, it creates the directories but not the files. Can anybody help?

    <?php

    class zipfile
    {
    var $datasec = array();
    var $ctrl_dir = array();
    var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
    var $old_offset = 0;

    function add_dir($name) {
    $name = str_replace("\\", "/", $name);

    $fr = "\x50\x4b\x03\x04";
    $fr .= "\x0a\x00";
    $fr .= "\x00\x00";
    $fr .= "\x00\x00";
    $fr .= "\x00\x00\x00\x00";

    $fr .= pack("V",0);
    $fr .= pack("V",0);
    $fr .= pack("V",0);
    $fr .= pack("v", strlen($name) );
    $fr .= pack("v", 0 );
    $fr .= $name;
    $fr .= pack("V", 0);
    $fr .= pack("V", 0);
    $fr .= pack("V", 0);

    $this -> datasec[] = $fr;
    $new_offset = strlen(implode("", $this->datasec));

    $cdrec = "\x50\x4b\x01\x02";
    $cdrec .="\x00\x00";
    $cdrec .="\x0a\x00";
    $cdrec .="\x00\x00";
    $cdrec .="\x00\x00";
    $cdrec .="\x00\x00\x00\x00";
    $cdrec .= pack("V",0);
    $cdrec .= pack("V",0);
    $cdrec .= pack("V",0);
    $cdrec .= pack("v", strlen($name) );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $ext = "\x00\x00\x10\x00";
    $ext = "\xff\xff\xff\xff";
    $cdrec .= pack("V", 16 );
    $cdrec .= pack("V", $this -> old_offset );
    $cdrec .= $name;

    $this -> ctrl_dir[] = $cdrec;
    $this -> old_offset = $new_offset;
    return;
    }

    function add_file($data, $name) {
    $name = str_replace("\\", "/", $name);
    $unc_len = strlen($data);
    $crc = crc32($data);
    $zdata = gzcompress($data);
    $zdata = substr ($zdata, 2, -4);
    $c_len = strlen($zdata);
    $fr = "\x50\x4b\x03\x04";
    $fr .= "\x14\x00";
    $fr .= "\x00\x00";
    $fr .= "\x08\x00";
    $fr .= "\x00\x00\x00\x00";
    $fr .= pack("V",$crc);
    $fr .= pack("V",$c_len);
    $fr .= pack("V",$unc_len);
    $fr .= pack("v", strlen($name) );
    $fr .= pack("v", 0 );
    $fr .= $name;
    $fr .= $zdata;
    $fr .= pack("V",$crc);
    $fr .= pack("V",$c_len);
    $fr .= pack("V",$unc_len);

    $this -> datasec[] = $fr;
    $new_offset = strlen(implode("", $this->datasec));

    $cdrec = "\x50\x4b\x01\x02";
    $cdrec .="\x00\x00";
    $cdrec .="\x14\x00";
    $cdrec .="\x00\x00";
    $cdrec .="\x08\x00";
    $cdrec .="\x00\x00\x00\x00";
    $cdrec .= pack("V",$crc);
    $cdrec .= pack("V",$c_len);
    $cdrec .= pack("V",$unc_len);
    $cdrec .= pack("v", strlen($name) );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("v", 0 );
    $cdrec .= pack("V", 32 );
    $cdrec .= pack("V", $this -> old_offset );

    $this -> old_offset = $new_offset;

    $cdrec .= $name;
    $this -> ctrl_dir[] = $cdrec;
    }

    function file() {
    $data = implode("", $this -> datasec);
    $ctrldir = implode("", $this -> ctrl_dir);

    return
    $data .
    $ctrldir .
    $this -> eof_ctrl_dir .
    pack("v", sizeof($this -> ctrl_dir)) .
    pack("v", sizeof($this -> ctrl_dir)) .
    pack("V", strlen($ctrldir)) .
    pack("V", strlen($data)) .
    "\x00\x00";
    }
    }

    // Test this class
    $zip = new zipfile();
    $zip->add_dir('Backup/');
    $zip->add_file('test', 'Backup/backup.txt');

    echo $zip->file();
    ?>
    Last edited by Research Names; 04-30-2004 at 05:16 AM.
    Quality web hosting PullHost.com

  2. #2
    Join Date
    May 2002
    Location
    UK
    Posts
    2,994
    All the code you need to create both directories and add files in, is located on the Zend site at

    http://www.zend.com/zend/spotlight/c...zip-files1.php

    Although there are few bugs in this code which can cause problems with Macs and gives warnings about file dates (as this code doesn't set them).

    However it's a very good resource to build your own zip classes.

  3. #3
    Can anybody show me a working version? The add_file function is not working!
    Quality web hosting PullHost.com

  4. #4
    I always use the pclzip class.

    Easy to use and has examples with it.

  5. #5
    Have a look to File_Archive... It's a pear package that lets you deal with a lot of different kind of archives
    poocl.la-grotte.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •