Php file online compression code. Php file online compression code classPHPZip {functionZip ($ dir, $ zipfilename) {if (@ function_exists (gzcompress) {$ curdirgetcwd (); if (is_array ($ dir )) {$ filelist $ dir;} e php file online compression code
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 );
}
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 an Unix timestamp to a four byte DOS date and time format
(Date
* In high two bytes, time in low two bytes allowing magnwing
Comparison ).
*
* @ Param integer the current Unix timestamp
*
* @ Return integer the 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 ['urs'] = 0;
$ Timearray ['minutes '] = 0;
$ Timearray ['seconds'] = 0;
} // End if
Return ($ timearray ['Year']-1980) <25) | ($ timearray ['mon'] <
21) | ($ timearray ['mday'] <16) |
($ Timearray ['urs'] <11) | ($ timearray ['minutes '] <5) |
($ Timearray ['seconds']> 1 );
} // End of the 'unix2dostime () 'method
/**
* Adds "file" to archive
*
* @ Param string file contents
* @ Param string name of the file in the archive (may contains
Path)
* @ Param integer the current timestamp
*
* @ Access public
*/
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 = "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 ($ 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 central directory record
$ Cdrec = "x50x4bx01x02 ";
$ Cdrec. = "x00x00"; // version made
$ 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
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
*/
Function file ()
{
$ Data = implode ('', $ this-> datasec );
$ Ctrldir = implode ('', $ this-> 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). // size of central dir
Pack ('v', strlen ($ data). // offset to start
Central dir
"X00x00"; //. zip file comment
Length
} // End of the 'file () 'method
}
Http://www.bkjia.com/PHPjc/444935.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444935.htmlTechArticlephp file online compression code class PHPZip {function Zip ($ dir, $ zipfilename) {if (@ function_exists ('gzcompress') {$ curdir = getcwd (); if (is_array ($ dir) {$ filelist = $ dir;} e...