php tutorial package (online decompression and online package download source)
This program supports file download decompression and online compression, so that we find examples online decompression and online packaging download function.
* /
//phpzip.class.php
class phpzip {
var $ datasec, $ ctrl_dir = array ();
var $ eof_ctrl_dir = "x50x4bx05x06x00x00x00x00";
var $ old_offset = 0; var $ dirs = array (".");
function get_list ($ zip_name) {
$ zip = @fopen ($ zip_name, 'rb');
if (! $ zip) return (0);
$ centd = $ this-> readcentraldir ($ zip, $ zip_name);
@rewind ($ zip);
@fseek ($ zip, $ centd ['offset']);
for ($ i = 0; $ i <$ centd ['entries']; $ i ++)
{
$ header = $ this-> readcentralfileheaders ($ zip);
$ header ['index'] = $ i; $ info ['filename'] = $ header ['filename'];
$ info ['stored_filename'] = $ header ['stored_filename'];
$ info ['size'] = $ header ['size']; $ info ['compressed_size'] = $ header ['compressed_size'];
$ info ['crc'] = strtoupper (dechex ($ header ['crc']));
$ info ['mtime'] = $ header ['mtime']; $ info ['comment'] = $ header ['comment'];
$ info ['folder'] = ($ header ['external'] == 0x41ff0010 || $ header ['external'] == 16)? 1: 0;
$ info ['index'] = $ header ['index']; $ info ['status'] = $ header ['status'];
$ ret [] = $ info; unset ($ header);
}
return $ ret;
}
function add ($ files, $ compact) {
if (! is_array ($ files [0])) $ files = array ($ files);
for ($ i = 0; $ files [$ i]; $ i ++) {
$ fn = $ files [$ i];
if (! in_array (dirname ($ fn [0]), $ this-> dirs))
$ this-> add_dir (dirname ($ fn [0]));
if (basename ($ fn [0]))
$ ret [basename ($ fn [0])] = $ this-> add_file ($ fn [1], $ fn [0], $ compact);
}
return $ ret;
}
function get_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)). "x00x00";
}
function add_dir ($ name) {
$ name = str_replace ("", "/", $ name);
$ fr = "x50x4bx03x04x0ax00x00x00x00x00x00x00x00x00";
pack ("v", 0) .pack ("v", 0) .pack ("v", strlen ($ name));
$ name.pack ("v", 0) .pack ("v", 0) .pack ("v", 0);
$ this -> datasec [] = $ fr;
$ new_offset = strlen (implode ("", $ this-> datasec));
$ cdrec = "x50x4bx01x02x00x00x0ax00x00x00x00x00x00x00x00x00";
pack ("v", 0) .pack ("v", 0) .pack ("v", strlen ($ name));
pack ("v", 0) .pack ("v", 0) .pack
$ ext = "xffxffxffxff";
$ cdrec. = pack ("v", 16) .pack ("v", $ this -> old_offset). $ name;
$ this -> ctrl_dir [] = $ cdrec;
$ this -> old_offset = $ new_offset;
$ this -> dirs [] = $ name;
}
function add_file ($ data, $ name, $ compact = 1) {
$ name = str_replace ('', '/', $ name);
$ dtime = dechex ($ this-> dostime ());
$ dtime [6]. $ dtime [7]. 'x'. $ dtime [4]. $ dtime [5]
$ dtime [2]. $ dtime [3]. 'x'. $ dtime [0]. $ dtime [1];
eval ('$ hexdtime = "'. $ hexdtime. '";');
if ($ compact)
$ fr = "x50x4bx03x04x14x00x00x00x08x00". $ hexdtime;
else $ fr = "x50x4bx03x04x0ax00x00x00x00x00". $ hexdtime;
$ unc_len = strlen ($ data); $ crc = crc32 ($ data);
if ($ compact) {
$ zdata = gzcompress ($ data); $ c_len = strlen ($ zdata);
$ zdata = substr (substr ($ zdata, 0, strlen ($ zdata) - 4), 2);
} else {
$ zdata = $ data;