IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

The Best Zip Function


dogtag
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 23
Joined: 7-June 07
Member No.: 44,336



Post #1 post Jun 7 2007, 02:41 PM
hi
my 6th code is very useful, you can zip your file by this:

CODE
<?
class dZip{
    var $filename;
    var $overwrite;
    
    var $zipSignature = "\x50\x4b\x03\x04"; // local file header signature
    var $dirSignature = "\x50\x4b\x01\x02"; // central dir header signature
    var $dirSignatureE= "\x50\x4b\x05\x06"; // end of central dir signature
    var $files_count  = 0;
    var $fh;
    
    Function dZip($filename, $overwrite=true){
        $this->filename  = $filename;
        $this->overwrite = $overwrite;
    }
    Function addDir($dirname, $fileComments=''){
        if(substr($dirname, -1) != '/')
            $dirname .= '/';
        $this->addFile(false, $dirname, $fileComments);
    }
    Function addFile($filename, $cfilename, $fileComments='', $data=false){
        if(!($fh = &$this->fh))
            $fh = fopen($this->filename, $this->overwrite?'wb':'a+b');
        
        // $filename can be a local file OR the data wich will be compressed
        if(substr($cfilename, -1)=='/'){
            $details['uncsize'] = 0;
            $data = '';
        }
        elseif(file_exists($filename)){
            $details['uncsize'] = filesize($filename);
            $data = file_get_contents($filename);
        }
        elseif($filename){
            echo "<b>Cannot add $filename. File not found</b><br>";
            return false;
        }
        else{
            $details['uncsize'] = strlen($filename);
            // DATA is given.. use it! :|
        }
        
        // if data to compress is too small, just store it
        if($details['uncsize'] < 256){
            $details['comsize'] = $details['uncsize'];
            $details['vneeded'] = 10;
            $details['cmethod'] = 0;
            $zdata = &$data;
        }
        else{ // otherwise, compress it
            $zdata = gzcompress($data);
            $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug (thanks to Eric Mueller)
            $details['comsize'] = strlen($zdata);
            $details['vneeded'] = 10;
            $details['cmethod'] = 8;
        }
        
        $details['bitflag'] = 0;
        $details['crc_32']  = crc32($data);
        
        // Convert date and time to DOS Format, and set then
        $lastmod_timeS  = str_pad(decbin(date('s')>=32?date('s')-32:date('s')), 5, '0', STR_PAD_LEFT);
        $lastmod_timeM  = str_pad(decbin(date('i')), 6, '0', STR_PAD_LEFT);
        $lastmod_timeH  = str_pad(decbin(date('H')), 5, '0', STR_PAD_LEFT);
        $lastmod_dateD  = str_pad(decbin(date('d')), 5, '0', STR_PAD_LEFT);
        $lastmod_dateM  = str_pad(decbin(date('m')), 4, '0', STR_PAD_LEFT);
        $lastmod_dateY  = str_pad(decbin(date('Y')-1980), 7, '0', STR_PAD_LEFT);
        
        # echo "ModTime: $lastmod_timeS-$lastmod_timeM-$lastmod_timeH (".date("s H H").")\n";
        # echo "ModDate: $lastmod_dateD-$lastmod_dateM-$lastmod_dateY (".date("d m Y").")\n";
        $details['modtime'] = bindec("$lastmod_timeH$lastmod_timeM$lastmod_timeS");
        $details['moddate'] = bindec("$lastmod_dateY$lastmod_dateM$lastmod_dateD");
        
        $details['offset'] = ftell($fh);
        fwrite($fh, $this->zipSignature);
        fwrite($fh, pack('s', $details['vneeded'])); // version_needed
        fwrite($fh, pack('s', $details['bitflag'])); // general_bit_flag
        fwrite($fh, pack('s', $details['cmethod'])); // compression_method
        fwrite($fh, pack('s', $details['modtime'])); // lastmod_time
        fwrite($fh, pack('s', $details['moddate'])); // lastmod_date
        fwrite($fh, pack('V', $details['crc_32']));  // crc-32
        fwrite($fh, pack('I', $details['comsize'])); // compressed_size
        fwrite($fh, pack('I', $details['uncsize'])); // uncompressed_size
        fwrite($fh, pack('s', strlen($cfilename)));   // file_name_length
        fwrite($fh, pack('s', 0));  // extra_field_length
        fwrite($fh, $cfilename);    // file_name
        // ignoring extra_field
        fwrite($fh, $zdata);
        
        // Append it to central dir
        $details['external_attributes']  = (substr($cfilename, -1)=='/'&&!$zdata)?16:32; // Directory or file name
        $details['comments']             = $fileComments;
        $this->appendCentralDir($cfilename, $details);
        $this->files_count++;
    }
    Function setExtra($filename, $property, $value){
        $this->centraldirs[$filename][$property] = $value;
    }
    Function save($zipComments=''){
        if(!($fh = &$this->fh))
            $fh = fopen($this->filename, $this->overwrite?'w':'a+');
        
        $cdrec = "";
        foreach($this->centraldirs as $filename=>$cd){
            $cdrec .= $this->dirSignature;
            $cdrec .= "\x0\x0";                  // version made by
            $cdrec .= pack('v', $cd['vneeded']); // version needed to extract
            $cdrec .= "\x0\x0";                  // general bit flag
            $cdrec .= pack('v', $cd['cmethod']); // compression method
            $cdrec .= pack('v', $cd['modtime']); // lastmod time
            $cdrec .= pack('v', $cd['moddate']); // lastmod date
            $cdrec .= pack('V', $cd['crc_32']);  // crc32
            $cdrec .= pack('V', $cd['comsize']); // compressed filesize
            $cdrec .= pack('V', $cd['uncsize']); // uncompressed filesize
            $cdrec .= pack('v', strlen($filename)); // file comment length
            $cdrec .= pack('v', 0);                // extra field length
            $cdrec .= pack('v', strlen($cd['comments'])); // file comment length
            $cdrec .= pack('v', 0); // disk number start
            $cdrec .= pack('v', 0); // internal file attributes
            $cdrec .= pack('V', $cd['external_attributes']); // internal file attributes
            $cdrec .= pack('V', $cd['offset']); // relative offset of local header
            $cdrec .= $filename;
            $cdrec .= $cd['comments'];
        }
        $before_cd = ftell($fh);
        fwrite($fh, $cdrec);
        
        // end of central dir
        fwrite($fh, $this->dirSignatureE);
        fwrite($fh, pack('v', 0)); // number of this disk
        fwrite($fh, pack('v', 0)); // number of the disk with the start of the central directory
        fwrite($fh, pack('v', $this->files_count)); // total # of entries "on this disk"
        fwrite($fh, pack('v', $this->files_count)); // total # of entries overall
        fwrite($fh, pack('V', strlen($cdrec)));     // size of central dir
        fwrite($fh, pack('V', $before_cd));         // offset to start of central dir
        fwrite($fh, pack('v', strlen($zipComments))); // .zip file comment length
        fwrite($fh, $zipComments);
        
        fclose($fh);
    }
    
    // Private
    Function appendCentralDir($filename,$properties){
        $this->centraldirs[$filename] = $properties;
    }
}

$newzip->addFile('dUnzip2.inc.php',  'class dUnzip2/dUnzip2.inc.php');
$newzip->addFile('documentation.txt','class dUnzip2/documentation.txt');
$newzip->addFile('dZip.inc.php',     'class dZip/dZip.inc.php');
$newzip->addFile('sample.php',       'sample.php');

# // Save the new file
$newzip->save();

?>
Go to the top of the page
+Quote Post
petdin11
no avatar
Newbie
*
Group: Members
Posts: 1
Joined: 21-November 07
Member No.: 53,447



Post #2 post Nov 21 2007, 02:02 PM
Hello,

I work with my friends on open source project (http://sourceforge.net/projects/wversions) developed in PHP. It can be used for exchange, file management and version control through the web.
I have found your source code with class dZip on internet and I would like to request your permission to use in it our project, if it is possible.
Looking forward for your response


Best regards

petdin
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   3 Albus Dumbledore 391 30th June 2006 - 05:07 PM
Last post by: serverph
No New Posts 3 MacFly 515 17th November 2004 - 06:24 AM
Last post by: Spectre
No New Posts   10 thablkpanda 1,290 27th February 2005 - 10:17 PM
Last post by: alexia
No New Posts   2 shadow skazi 859 1st March 2005 - 04:16 PM
Last post by: maddog39
No New Posts   2 kvarnerexpress 546 9th April 2005 - 04:17 AM
Last post by: dexter
No New Posts   4 leiaah 582 18th April 2005 - 01:01 AM
Last post by: leiaah
No New Posts   4 beeseven 694 18th April 2005 - 12:38 AM
Last post by: snlildude87
No New Posts   3 snlildude87 910 19th April 2005 - 11:26 AM
Last post by: snlildude87
No New Posts   1 kvarnerexpress 988 4th November 2005 - 04:16 AM
Last post by: mcfly
No New Posts   1 HmmZ 520 20th June 2005 - 02:29 AM
Last post by: SystemWisdom
No New Posts   1 nickmealey 518 21st June 2005 - 01:30 AM
Last post by: beeseven
No New Posts   1 palladin 885 6th July 2006 - 11:07 AM
Last post by: DeveloperX
No New Posts   0 hamza12 73 28th October 2008 - 07:37 AM
Last post by: hamza12
No New Posts   2 kvarnerexpress 528 20th October 2005 - 02:34 AM
Last post by: dul
No New Posts   2 kvarnerexpress 1,425 16th September 2005 - 10:46 PM
Last post by: moldboy


 



RSS Open Discussion Time is now: 10th January 2009 - 02:39 AM