PHP file compression Phpzip Class usage Example

Source: Internet
Author: User
Tags bit set crc32 getdate pack php file strlen time and date zip

This article illustrates the Phpzip class usage of PHP file compression. Share to everyone for your reference. Specifically as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 181 182 183 184 185 186 187 188 189 190 191 192 193-194 195 <?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"); $co Ntent = 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; The function getfilelist ($dir) {if (file_exists ($dir)) {$args = Func_get_args (); $pref = $args [1]; $DH = Opendir ($dir); WH Ile$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); 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 byte DOS date and time format (date * in high two bytes, time in low two bytes allowing magnitude comparison). * @param integer The current Unix timestamp * * @return integer The current date in a four byte DOS format * * @access P Rivate/function unix2dostime ($unixtime = 0) {$timearray = ($unixtime = = 0)? getdate (): getdate ($unixtime); if ($time array[' Year '] < 1980} {$timearray [' year '] = 1980; $timearray [' mon '] = 1; $timearray [' mday '] = 1; $timearray [' hours '] = 0; $timearray [' minutes '] = 0; $timearray [' seconds '] = 0; }//End IfReturn (($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 name The "File in" archive (may contains the path) * @param an integer the current timestamp * * @access public/function A Ddfile ($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 add to the $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 local header $this-> old_offset = $new _offset; $cdrec. = $name; Optional extra field, file comment goes here//save to $this-> ctrl_dir[] = $cdrec; //End of the ' AddFile () ' method/** * Dumps out file * * @return String The zipped file * * @access public * * function File () {$data = Implode (', $this-> datasec); $ctrldir = Implode (", $this-&Gt CTRL_DIR);   Return $data. $ctrldir. $this-> Eof_ctrl_dir. 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)). The size of the???. Dir Pack (' V ', strlen ($data)). Offset to start of the "x00x00"; . zip file Comment Length}//End of the ' file () ' method}//End of the ' Phpzip ' class?>

How to use:

?

1 2 3 4 5 6 7 8 9 10 11 <?php $z = new Phpzip (); New to build a zip class//Method One: $z-> zip ("", "Out1.zip"); Add all files under current directory and subdirectory/method 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 specified directory?>

I hope this article will help you with your PHP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.