This article mainly introduces the definition and use of the Phpzip class, the interested friend's reference, hope to be helpful to everyone.
This article describes the PHP file compression of the Phpzip class usage, specifically as follows:
<?php////Phpzip v1.2 by Sext (sext@neud.net)////makes zip archive////Based on "Zip file Creation class", uses zlib/ Class Phpzip{function Zip ($dir, $zipfilename) {if (@function_exists (' gzcompress ')) {$curdir = GETCWD (); if (Is_array ($dir)) {$filelist = $dir; } else {$filelist = $this-Getfilelist ($dir); } if ((!empty ($dir)) && (!is_array ($dir)) && (File_exists ($dir))) chdir ($dir); else ChDir ($curdir); if (count ($filelist) >0) {foreach ($filelist as $filename) {if (Is_file ($filename)) { $FD = fopen ($filename, "R"); $content = Fread ($FD, FileSize ($filename)); Fclose ($FD); if (Is_array ($dir)) $filename = basename ($filename); $this-AddFile ($content, $filename); }} $out = $this file (); ChDir ($curdir); $fp = fopen ($zipfilename, "w"); Fwrite ($fp, $out, strlen ($out)); Fclose ($FP); } return 1; } else return 0;} function Getfilelist ($dir) {if (file_exists ($dir)) {$args = Func_get_args (); $pref = $args [1]; $DH = Opendir ($dir); while ($files = Readdir ($DH)) {if ($files! = ".") && ($files! = "...")) {if (Is_dir ($dir. $files)) {$curdir = GETCWD (); ChDir ($dir. $files); $file = Array_merge ($file, $this, Getfilelist ("", "$pref $files/")); ChDir ($curdir); The Else $file []= $pref. $files; }} closedir ($DH); } return $file;} var $datasec = array (), var $ctrl _dir = Array (), var $eof _ctrl_dir = "x50x4bx05x06x00x00x00x00", var $old _offset = 0;/** * Converts a Unix timestamp to a four bytes DOS date and time format (date * in high, bytes, time in low, and both bytes all Owing magnitude comparison). * * @param integer The current Unix timestamp * * @return integer ' current date ' in a four byte DOS format * * @ AccESS Private */function unix2dostime ($unixtime = 0) {$timearray = ($unixtime = = 0)? getdate (): getdate ($unixtime); if ($timearray [' Year '] < 1980} {$timearray [' year '] = 1980; $timearray [' mon '] = 1; $timearray [' mday '] = 1; $timearray [' hours '] = 0; $timearray [' minutes '] = 0; $timearray [' seconds '] = 0; }//End If return (($timearray [' Year ']-1980) << 25) | ($timearray [' mon '] << 21) | ($timearray [' Mday '] << 16) | ($timearray [' hours '] << 11) | ($timearray [' minutes '] << 5) | ($timearray [' seconds '] >> 1);} End of the ' unix2dostime () ' method/** * Adds "file" to archive * * @param string file contents * @param string NA Me of the The archive (may contains the path) * @param an integer the current timestamp * * @access public */FU Nction AddFile ($data, $name, $time = 0) {$name = Str_replace (', '/', $name); $dtime = Dechex ($this->unix2dostime ($time)); $hexdtime = ' x '. $dtime [6] . $dtime [7]. ' X '. $dtime [4]. $dtime [5]. ' X '. $dtime [2]. $dtime [3]. ' X '. $dtime [0]. $dtime [1]; Eval (' $hexdtime = '. $hexdtime. '";'); $FR = "x50x4bx03x04"; $fr. = "x14x00"; Ver needed to extract $fr. = "x00x00"; Gen purpose bit flag $fr. = "x08x00"; Compression method $fr. = $hexdtime; Last mod time and date//"Local file Header" segment $unc _len = strlen ($data); $CRC = CRC32 ($data); $zdata = gzcompress ($data); $c _len = strlen ($zdata); $zdata = substr (substr ($zdata, 0, strlen ($zdata)-4), 2); Fix CRC bug $fr. = Pack (' V ', $CRC); CRC32 $fr. = Pack (' V ', $c _len); Compressed filesize $fr. = Pack (' V ', $unc _len); Uncompressed filesize $fr. = Pack (' V ', strlen ($name)); Length of filename $fr. = Pack (' V ', 0); Extra field length $fr. = $name; "File Data" segment $fr. = $zdata; "Data Descriptor" segment (optional but necesSary if archive is not//served as file) $fr. = Pack (' V ', $CRC); CRC32 $fr. = Pack (' V ', $c _len); Compressed filesize $fr. = Pack (' V ', $unc _len); Uncompressed FileSize//Add this entry to array $this-datasec[] = $FR; $new _offset = strlen (Implode (", $this->datasec)); Now the add to central directory record $cdrec = "x50x4bx01x02"; $cdrec. = "x00x00"; Version made by $cdrec. = "x14x00"; Version needed to extract $cdrec. = "x00x00"; Gen purpose bit flag $cdrec. = "x08x00"; Compression method $cdrec. = $hexdtime; Last mod time & date $cdrec. = Pack (' V ', $CRC); CRC32 $cdrec. = Pack (' V ', $c _len); Compressed filesize $cdrec. = Pack (' V ', $unc _len); Uncompressed filesize $cdrec. = Pack (' V ', strlen ($name)); Length of filename $cdrec. = Pack (' V ', 0); Extra field length $cdrec. = Pack (' V ', 0); File Comment LengtH $cdrec. = Pack (' V ', 0); Disk number start $cdrec. = Pack (' V ', 0); Internal file attributes $cdrec. = Pack (' V ', 32); External file attributes-' archive ' bit set $cdrec. = Pack (' V ', $this-old_offset); Relative offset of the local header $this-old_offset = $new _offset; $cdrec. = $name; Optional extra field, file comment goes here//save to Central directory $this, ctrl_dir[] = $cdrec;} End of the ' AddFile () ' method/** * dumps out file * * @return String The zipped file * * @access public */funct Ion file () {$data = Implode (', $this-datasec); $ctrldir = Implode (", $this-Ctrl_dir); Return $data. $ctrldir. Eof_ctrl_dir, $this. Pack (' V ', sizeof ($this-Ctrl_dir)). Total # of Entries "on this disk" pack (' V ', sizeof ($this-Ctrl_dir)). Total # of Entries Overall pack (' V ', strlen ($ctrldir)). Size of Central dir pack (' V ', StrleN ($data)). Offset to start of the central dir "x00x00"; . zip file Comment Length}//End of the ' file () ' method}//End of the ' Phpzip ' class?>
How to use:
<?php$z = new Phpzip (); Create a new Zip class//Method One: $z-Zip ("", "Out1.zip"); Add all files//methods under current directory and subdirectories two: $files =array (' 1.txt ', ' gb.txt '), $files []= ' 5.txt '; $z-Zip ($files, "out2.zip"); Add File List//method three: $z-Zip ("/usr/local/sext/", "Out3.zip"); Add the specified directory?>
Summary: The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Php method for recursive operation of files
How PHP accepts files and obtains suffix names
PHP Processing session Function Summary sharing