Copy codeThe Code is as follows:
<? Php
/*
$ Id: PHPZip. php
*/
Class PHPZip {
Var $ datasec = array ();
Var $ ctrl_dir = array ();
Var $ eof_ctrl_dir = "\ x50 \ x4b \ x05 \ x06 \ x00 \ x00 \ x00 \ x00 ";
Var $ old_offset = 0;
Function Zip ($ dir, $ zipfilename ){
If (@ function_exists ('gzcompress ')){
@ Set_time_limit ("0 ");
$ This-> openFile ($ dir, $ dir );
$ Out = $ this-> filezip ();
$ Fp = fopen ($ zipfilename, "w ");
Fwrite ($ fp, $ out, strlen ($ out ));
Fclose ($ fp );
}
}
Function openFile ($ path, $ zipName ){
$ Temp_path = $ path;
$ Temp_zip_path = $ zipName;
$ ZipDir = $ zipName;
If ($ handle = @ opendir ($ path )){
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = '.' And $ file! = '..'){
If (ereg ('\.', $ file. @ basename ())){
$ Fd = fopen ($ path. '/'. $ file, "r ");
$ FileValue = @ fread ($ fd, 1024000 );
Fclose ($ fd );
$ This-> addFile ($ fileValue, $ zipName. '/'. $ file );
} Else {
$ This-> openFile ($ path. '/'. $ file, $ zipName. '/'. $ file );
}
}
}
$ ZipName = $ temp_zip_path;
$ Path = $ temp_path;
Closedir ($ handle );
}
}
Function unix2DosTime ($ unixtime = 0 ){
$ Timearray = ($ unixtime = 0 )? Getdate (): getdate ($ unixtime );
If ($ timearray ['Year'] <1980 ){
$ Timearray ['Year'] = 1980;
$ Timearray ['mon'] = 1;
$ Timearray ['mday'] = 1;
$ Timearray ['urs'] = 0;
$ Timearray ['minutes '] = 0;
$ Timearray ['seconds'] = 0;
}
Return ($ timearray ['Year']-1980) <25) | ($ timearray ['mon'] <21) | ($ timearray ['mday'] <16) |
($ Timearray ['urs'] <11) | ($ timearray ['minutes '] <5) | ($ timearray ['seconds']> 1 );
}
Function 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 = "\ x50 \ x4b \ x03 \ x04 ";
$ Fr. = "\ x14 \ x00 ";
$ Fr. = "\ x00 \ x00 ";
$ Fr. = "\ x08 \ x00 ";
$ Fr. = $ hexdtime;
$ Unc_len = strlen ($ data );
$ Crc = crc32 ($ data );
$ Zdata = gzcompress ($ data );
$ C_len = strlen ($ zdata );
$ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2 );
$ 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. = $ hexdtime;
$ 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 filezip (){
$ 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 ";
}
}
?>