PHP package ZIP file, complete instance (class attached, call method attached), has been tested OK

Source: Internet
Author: User
Tags bit set crc32

 

<? PHP
$ Z = new phpzip ();
$ File = array('itokit.jpg ');
$ Zipname = './itokit_com/'. Time (). '.zip ';
$ Z-> zip ($ file, $ zipname );

/**
The following are other methods that call this class:
$ Z = new phpzip (); // create a zip class

Method 1:
$ Z-> zip ("", "out1.zip"); // Add all files under the current directory and subdirectory

Method 2:
Using filespolicarray('1.txt', 'gb.txt ');
Using files000000000000'5.txt ';
$ Z-> zip ($ files, "out2.zip"); // Add a file list

Method 3:
$ Z-> zip ("/usr/local/sext/", "out3.zip"); // Add a specified directory
*/

#
# Phpzip v1.2 by sext (sext@neud.net) 2002-11-18
# (Changed: 2003-001)
#
# Makes ZIP Archive
#
# Based on "zip file creation class", uses zlib
#
# Examples in sample1.php, sample2.php and sample3.php
#

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 = "\ x50 \ x4b \ x05 \ x06 \ x00 \ x00 \ x00 \ x00 ";
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 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 );
$ 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;
} // 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

} // End of the 'phpzip' class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.