This php class can add files to the array one by one, and finally package the added files into zip files.
- /* $ Id: zip. lib. php, v 1.1 2004/02/14 15:21:18 anoncvs_tusedb Exp $ */
- // Vim: expandtab sw = 4 ts = 4 sts = 4:
-
-
- /**
- * Zip file creation class.
- * Makes zip files.
- *
- * Last Modification and Extension:
- *
- * Hasin Hayder
- * HomePage: www.hasinme.info
- * Email: countdraculla@gmail.com
- * IDE: PHP Designer 2005
- *
- *
- * Originally Based on:
- *
- * Http://www.zend.com/codex.php? Id = 535 & single = 1
- * By Eric Mueller
- *
- * Http://www.zend.com/codex.php? Id = 470 & single = 1
- * By Denis125
- *
- * A patch from Peter Listiak For last modified
- * Date and time of the compressed file
- *
- * Official ZIP file format: http://www.pkware.com/appnote.txt
- *
- * @ Access public
- */
- Class zipfile
- {
- /**
- * Array to store compressed data
- *
- * @ Public array $ datasec
- */
- Public $ datasec = array ();
-
- /**
- * Central directory
- *
- * @ Public array $ ctrl_dir
- */
- Public $ ctrl_dir = array ();
-
- /**
- * End of central directory record
- *
- * @ Public string $ eof_ctrl_dir
- */
- Public $ eof_ctrl_dir = "\ x50 \ x4b \ x05 \ x06 \ x00 \ x00 \ x00 \ x00 ";
-
- /**
- * Last offset position
- *
- * @ Public integer $ old_offset
- */
- Public $ 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 magn1_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 the 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 = "\ x50 \ x4b \ x03 \ x04 ";
- $ Fr. = "\ x14 \ x00"; // ver needed to extract
- $ Fr. = "\ x00 \ x00"; // gen purpose bit flag
- $ Fr. = "\ x08 \ x00"; // compression method
- $ Fr. = $ hexdtime; // last mod time and date
-
- // "Local file header" segment
- $ Unc_len = strlen ($ data );
- $ Crc = crc32 ($ data );
- $ Zdata = gzcompress ($ data );
- $ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the crc bug
- $ C_len = strlen ($ zdata );
- $ 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;
-
- // Now add to central directory record
- $ Cdrec = "\ x50 \ x4b \ x01 \ x02 ";
- $ Cdrec. = "\ x00 \ x00"; // version made
- $ Cdrec. = "\ x14 \ x00"; // version needed to extract
- $ Cdrec. = "\ x00 \ x00"; // gen purpose bit flag
- $ Cdrec. = "\ x08 \ x00"; // 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 + = strlen ($ fr );
-
- $ 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 of central dir
- "\ X00 \ x00"; //. zip file comment length
- } // End of the 'file () 'method
-
-
- /**
- * A Wrapper of original addFile Function
- *
- * Created By Hasin Hayder at 29th Jan, AM
- *
- * @ Param array An Array of files with relative/absolute path to be added in Zip File
- *
- * @ Access public
- */
- Function addFiles ($ files/* Only Pass Array */)
- {
- Foreach ($ files as $ file)
- {
- If (is_file ($ file) // directory check
- {
- $ Data = implode ("", file ($ file ));
- $ This-> addFile ($ data, $ file );
- }
- }
- }
-
- /**
- * A Wrapper of original file Function
- *
- * Created By Hasin Hayder at 29th Jan, AM
- *
- * @ Param string Output file name
- *
- * @ Access public
- */
- Function output ($ file)
- {
- $ Fp = fopen ($ file, "w ");
- Fwrite ($ fp, $ this-> file ());
- Fclose ($ fp );
- }
-
-
-
- } // End of the 'zipfile' class
|