- /*
- File name:/include/zip. php
- Author: hsf-e 2009/04/15
- */
- Class PHPZip {
- Var $ dirInfo = array ("0", "0 ");
- Var $ rootDir = '';
- Var $ datasec = array ();
- Var $ ctrl_dir = array ();
- Var $ eof_ctrl_dir = "/x50/x4b/x05/x06/x00/x00/x00/x00 ";
- Var $ old_offset = 0;
- Function downloadZip (){
- CreateZip ($ dir, $ zipfilename, true );
- }
- Function createZip ($ dir, $ zipfilename, $ autoDownload = false ){
- If (@ function_exists ('gzcompress ')){
- @ Set_time_limit ("0 ");
- If (is_array ($ dir )){
- $ Fd = fopen ($ dir, "r ");
-
- $ FileValue = fread ($ fd, filesize ($ filename ));
- Fclose ($ fd );
- If (is_array ($ dir) $ filename = basename ($ dir );
- $ This-> addFile ($ fileValue, "$ filename ");
- } Else {
- $ This-> dirTree ($ dir, $ dir );
- }
- $ Zipfilenametemp = time (). $ zipfilename;
- $ Out = $ this-> filezip ();
- $ Fp = fopen ($ zipfilenametemp, "w ");
- Fwrite ($ fp, $ out, strlen ($ out ));
- Fclose ($ fp );
- $ Filesize = filesize ($ zipfilenametemp );
- If ($ filesize <104857600 ){
- If ($ autoDownload ){
- Header ("Content-type: application/octet-stream ");
- Header ("Content-disposition: attachment; filename =". $ zipfilename );
- }
- Echo $ this-> filezip ();
- } Else {
- Echo "create zip error! ";
- }
- Unlink ($ zipfilenametemp );
- }
- }
- // Get dir tree ..
- Function dirTree ($ directory, $ rootDir ){
- Global $ _ SERVER, $ dirInfo, $ rootDir;
- $ FileDir = $ rootDir;
- $ MyDir = dir ($ directory );
- While ($ file = $ myDir-> read ()){
-
-
- If (is_dir ("$ directory/$ file") and $ file! = "." And $ file! = ".."){
- $ DirInfo [0] ++;
- $ RootDir = "$ fileDir $ file /";
- $ This-> addFile (''," $ rootDir ");
- // Go on n's folders
- $ This-> dirTree ("$ directory/$ file", $ rootDir );
- } Else {
- If ($ file! = "." And $ file! = ".."){
- $ DirInfo [1] ++;
- // $ Fd = fopen ("$ directory/$ file", "r ");
- $ FileValue = file_get_contents ("$ directory/$ file ");
- // Fclose ($ fd );
- $ This-> addFile ($ fileValue, "$ fileDir $ file ");
- }
- }
- }
- $ MyDir-> close ();
- }
- 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 );
- }
- 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 );
-
-
- $ C_len = strlen ($ zdata );
- $ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the 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 = "/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 = $ new_offset;
- $ Cdrec. = $ name;
- // Optional extra field, file comment goes here
- // Save to central directory
- $ 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). // 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
- }
- }
- ?>
-
- There is another relatively simple
-
- [Php] view plaincopy
-
- Class createzip
- {
- /* @ Creates a compressed zip file: function for compressing multiple files into one zip file
- * @ $ Files array type instance array ("1.jpg"," 2.jpg ");
- * @ Destination the path of the target file, for example, "c:/androidyue.zip"
- * @ $ Overwrite indicates whether to overwrite the same object as the target object.
- * @ Recorded By Androidyue
- * @ Blog: http://thinkblog.sinaapp.com
- */
- Function create_zip ($ files = array (), $ destination = '', $ overwrite = false)
- {
- // If the zip file already exists and is set to not overwrite, false is returned.
- If (file_exists ($ destination )&&! $ Overwrite) {return false ;}
- $ Valid_files = array ();
- // If files were passed in...
- // Obtain the real and valid file name
- If (is_array ($ files )){
- // Cycle through each file
- Foreach ($ files as $ file ){
- // Make sure the file exists
- If (file_exists ($ file )){
- $ Valid_files [] = $ file;
- }
- }
- }
- // If a real and valid file exists
- If (count ($ valid_files ))
- {
- // Create the archive
- $ Zip = new ZipArchive ();
- // Open the file and overwrite it if it already exists. otherwise, create
- If ($ zip-> open ($ destination, $ overwrite? ZIPARCHIVE: OVERWRITE: ZIPARCHIVE: CREATE )! = True ){
- Return false;
- }
- // Add a file to the compressed file
- Foreach ($ valid_files as $ file ){
- $ Zip-> addFile ($ file, $ file );
- }
- // Close the file
- $ Zip-> close ();
- // Check whether the file exists
- Return file_exists ($ destination );
- }
- Else {
- // If no valid file exists, false is returned.
- Return false;
- }
- }
-
- }
- /****
- // Test the function
- $ Files = array ('temp. php', 'Test. php ');
- Create_zip ($ files, 'myzipfile.zip ', true );
- ****/
- ?>
|