Sometimes you need to output all the files in a folder on the server, such as backing up a website, and use the following PHP function. The function library code PHPZip is: (copy the following code to save it for calling )? ClassPHPZip {Private $ StrPath... @ UpLoad; Private $ datasecarray (); Private $ ctrl_dirarray (); Pr
Sometimes you need to output all the files in a folder on the server, such as backing up a website, and use the following PHP function. The function library code PHPZip is: (copy the following code to save it for calling )? Class PHPZip {Private $ StrPath = ".../@ UpLoad"; Private $ datasec = array (); Private $ ctrl_dir = array (); Pr
Sometimes you need to output all the files in a folder on the server, such as backing up a website, and use the following PHP function.
The function library code PHPZip is: (copy the following code to save it for calling)
StrPath); header ('content-Encoding: none'); header ('content-Type: application/zip'); header ('content-Disposition: attachment; filename = '. str_Replace (". zip "," ", basename({zipfilename}.'.zip '); header ('pragma: no-cache'); header ('expires: 0'); print ($ out );} // Save the data after compression on the server. By default, the Function SaveFile ($ curdir, $ zipfilename, $ out) {$ curdir = Str_iReplace (basename ($ curdir ), "", $ curdir); chdir ($ curdir. $ this-> StrPath); $ fp = fopen ($ zipfilename, "wb"); fwrite ($ fp, $ out, strlen ($ out); fclose ($ fp );} // function purpose: compress the folder and perform recursive operations layer by layer // detailed description of parameters: $ dir directory to be compressed, $ zipfilename compressed file name and storage path, $ Todo processing method 1 download 2 save the file on the server Function ZipDir ($ dir, $ zipfilename, $ Todo) {IF (! @ Function_exists ('gzcompress') {Return 0 ;}@ set_time_limit ("0"); $ this-> openFile ($ dir, $ zipfilename ); $ out = $ this-> filezip (); Switch ($ Todo) {Case "1": $ this-> DownLoad (_ FILE __, $ zipfilename, $ out ); break; Case "2": $ this-> SaveFile (_ FILE __, $ zipfilename, $ out); Break ;}// function purpose: compress the FILE, it can be a single file or an array with multiple parameters. // detailed explanation: $ file files to be compressed (can be an array), $ zipfilename compressed file names and storage paths, $ Todo processing method 1 download 2 save the file in the server Function ZipFile ($ file, $ z Ipfilename, $ Todo) {IF (! @ Function_exists ('yzcompress') {Return 0;} IF (is_array ($ file) {Foreach ($ file as $ filename) {IF (is_file ($ filename )) {$ fd = fopen ($ filename, "rb"); $ content = @ fread ($ fd, filesize ($ filename); fclose ($ fd ); $ filename = basename ($ filename); $ this-> addFile ($ content, $ filename) ;}} Else {IF (is_file ($ file )) {$ fd = fopen ($ file, "rb"); $ content = @ fread ($ fd, filesize ($ file); fclose ($ fd); $ file = basena Me ($ file); $ this-> addFile ($ content, $ file) ;}}$ out = $ this-> filezip (); Switch ($ Todo) {Case "1": $ this-> DownLoad (_ FILE __, $ zipfilename, $ out); Break; Case "2 ": $ this-> SaveFile (_ FILE __, $ zipfilename, $ out); Break ;}// recursively retrieve the Function openFile ($ path, $ zipName) of the FILE in the lower-level folder) {$ temp_path = $ path; $ temp_zip_path = $ 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, $ path. '/'. $ file);} Else {$ this-> openFile ($ path. '/'. $ file, $ zipName. '/'. $ file) ;}}$ zipName = $ temp_zip_path; $ path = $ temp_path; closedir ($ handle) ;}} Function addFile ($ data, $ name, $ time = 0) {$ name = str_replace ('\', '/', $ name); $ name = str_replace ('. /',', s Tr_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 [] = $ cdr Ec;} 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";} Function unix2DosTime ($ unixtime = 0) {$ timearray = ($ unixtime = 0 )? Getdate (): getdate ($ unixtime); IF ($ timearray ['Year'] <1980) {$ timearray ['Year'] = 1980; $ timearray ['mon'] = 1; $ timearray ['mday'] = 1; $ timearray ['hours'] = 0; $ timearray ['minutes '] = 0; $ timearray ['seconds'] = 0;} Return ($ timearray ['Year']-1980) <25) | ($ timearray ['mon'] <21) | ($ timearray ['mday'] <16) | ($ timearray ['hours'] <11) | ($ timearray ['minutes '] <5) | ($ timearray ['seconds']> 1 );} /* Partial purpose: extract the file parameters: $ zn zip file and path to be decompressed, $ to decompressed destination */Function Unzip_File ($ zn, $, $ index = Array (-1) {$ OK = 0; $ zip = @ fopen ($ zn, 'rb'); IF (! $ Zip) {Return (-1) ;}$ cdir = $ this-> ReadCentralDir ($ zip, $ zn); $ pos_entry = $ cdir ['offset']; IF (! Is_array ($ index) {$ index = array ($ index) ;}for ($ I = 0; $ index [$ I]; $ I ++) {IF (intval ($ index [$ I])! = $ Index [$ I] | $ index [$ I]> $ cdir ['entries']) {Return (-1) ;}} For ($ I = 0; $ I <$ cdir ['entries']; $ I ++) {@ fseek ($ zip, $ pos_entry); $ header = $ this-> ReadCentralFileHeaders ($ zip ); $ header ['index'] = $ I; $ pos_entry = ftell ($ zip); @ rewind ($ zip); fseek ($ zip, $ header ['offset']); IF (in_array ("-1", $ index) | in_array ($ I, $ index )) {$ stat [$ header ['filename'] = $ this-> ExtractFile ($ header, $ to, $ zip) ;}} fclose ($ Zip); Return $ stat;} // obtain the file information in the compressed package. Function get_List ($ zip_name) {$ zip = @ fopen ($ zip_name, 'rb '); IF (! $ Zip) {Return (0) ;}$ centd = $ this-> ReadCentralDir ($ zip, $ zip_name); @ rewind ($ zip); @ fseek ($ zip, $ centd ['offset']); For ($ I = 0; $ I <$ centd ['entries']; $ I ++) {$ header = $ this-> ReadCentralFileHeaders ($ zip); $ header ['index'] = $ I; $ info ['filename'] = $ header ['filename']; $ info ['stored _ filename'] = $ header ['stored _ filename']; $ info ['SIZE'] = $ header ['SIZE']; $ info ['compressed _ size'] = $ header ['compressed _ Size ']; $ info ['crc'] = strtoupper (dechex ($ header ['crc ']); $ info ['mtime'] = $ header ['mtime']; $ info ['comment'] = $ header ['comment']; $ info ['folder'] = ($ header ['external '] = 0x41FF0010 | $ header ['external'] = 16 )? 1:0; $ info ['index'] = $ header ['index']; $ info ['status'] = $ header ['status']; $ ret [] = $ info; unset ($ header);} Return $ ret;} Function ReadFileHeader ($ zip) {$ binary_data = fread ($ zip, 30 ); $ data = unpack ('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len ', $ binary_data ); $ header ['filename'] = fread ($ zip, $ data ['filename _ len']); IF ($ data ['extra _ l En ']! = 0) {$ header ['extra '] = fread ($ zip, $ data ['extra _ len']);} else {$ header ['extra '] = '';} $ header ['compression'] = $ data ['compression']; $ header ['SIZE'] = $ data ['SIZE']; $ header ['compressed _ size'] = $ data ['compressed _ size']; $ header ['crc '] = $ data ['crc']; $ header ['flag'] = $ data ['flag']; $ header ['mdate'] = $ data ['mdate']; $ header ['mtime'] = $ data ['mtime']; IF ($ header ['mdate'] & $ header ['mtime']) {$ hou R = ($ header ['mtime'] & 0xF800)> 11; $ minute = ($ header ['mtime'] & 0x07E0)> 5; $ seconde = ($ header ['mtime'] & 0x001F) * 2; $ year = ($ header ['mdate'] & 0xFE00)> 9) + 1980; $ month = ($ header ['mdate'] & 0x01E0)> 5; $ day = $ header ['mdate'] & 0x001F; $ header ['mtime'] = mktime ($ hour, $ minute, $ seconde, $ month, $ day, $ year );} else {$ header ['mtime'] = time ();} $ header ['stored _ filename'] = $ header ['filename' ]; $ Header ['status'] = "OK"; Return $ header;} Function ReadCentralFileHeaders ($ zip) {$ binary_data = fread ($ zip, 46 ); $ header = unpack ('vchkid/vid/vversion/kernel/vflag/vcompression/vmtime/vmdate/Vcrc/kernel/Vsize/kernel/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/ voffset ', $ binary_data); IF ($ header ['filename _ len']! = 0) $ header ['filename'] = fread ($ zip, $ header ['filename _ len']); Else $ header ['filename'] = ''; IF ($ header ['extra _ len']! = 0) $ header ['extra '] = fread ($ zip, $ header ['extra _ len']); Else $ header ['extra'] = ''; IF ($ header ['comment _ len']! = 0) $ header ['comment'] = fread ($ zip, $ header ['comment _ len']); Else $ header ['comment'] = ''; IF ($ header ['mdate'] & $ header ['mtime']) {$ hour = ($ header ['mtime'] & 0xF800)> 11; $ minute = ($ header ['mtime'] & 0x07E0)> 5; $ seconde = ($ header ['mtime'] & 0x001F) * 2; $ year = ($ header ['mdate'] & 0xFE00)> 9) + 1980; $ month = ($ header ['mdate'] & 0x01E0)> 5; $ day = $ header ['mdate'] & 0x001F; $ header ['mtim E '] = mktime ($ hour, $ minute, $ seconde, $ month, $ day, $ year );} else {$ header ['mtime'] = time ();} $ header ['stored _ filename'] = $ header ['filename']; $ header ['status'] = 'OK'; IF (substr ($ header ['filename'],-1) = '/') $ header ['external '] = 0x41FF0010; Return $ header;} Function ReadCentralDir ($ zip, $ zip_name) {$ size = filesize ($ zip_name ); IF ($ size <277) {$ maximum_size = $ size;} Else {$ maximum_size = 2 77 ;}@ fseek ($ zip, $ size-$ maximum_size); $ pos = ftell ($ zip); $ bytes = 0x00000000; While ($ pos <$ size) {$ byte = @ fread ($ zip, 1); $ bytes = ($ bytes <8) | Ord ($ byte); IF ($ bytes = 0x504b0506) {$ pos ++; break;} $ pos ++;} $ data = unpack ('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size ', fread ($ zip, 18); IF ($ data ['comment _ size']! = 0) {$ centd ['comment'] = fread ($ zip, $ data ['comment _ size']);} else {$ centd ['comment'] = '';} $ centd ['entries'] = $ data ['entries']; $ centd ['disk _ entries'] = $ data ['disk _ entries']; $ centd ['offset'] = $ data ['offset']; $ centd ['disk _ start'] = $ data ['disk _ start']; $ centd ['SIZE'] = $ data ['SIZE']; $ centd ['disk'] = $ data ['disk']; Return $ centd;} Function ExtractFile ($ header, $ to, $ zip) {$ header = $ this-> re Adfileheader ($ zip); IF (substr ($ to,-1 )! = "/") $ To. = "/"; IF (! @ Is_dir ($ to) @ mkdir ($ to, 0777); $ pth = explode ("/", dirname ($ header ['filename']); for ($ I = 0; isset ($ pth [$ I]); $ I ++) {IF (! $ Pth [$ I]) continue; $ pthss. = $ pth [$ I]. "/"; IF (! Is_dir ($ to. $ pthss) @ mkdir ($ to. $ pthss, 0777);} IF (! ($ Header ['external '] = 0x41FF0010 )&&! ($ Header ['external '] = 16) {IF ($ header ['compression'] = 0) {$ fp = @ fopen ($. $ header ['filename'], 'wb '); IF (! $ Fp) Return (-1); $ size = $ header ['compressed _ size']; While ($ size! = 0) {$ read_size = ($ size <2048? $ Size: 2048); $ buffer = fread ($ zip, $ read_size); $ binary_data = pack ('A '. $ read_size, $ buffer); @ fwrite ($ fp, $ binary_data, $ read_size); $ size-= $ read_size;} fclose ($ fp); touch ($. $ header ['filename'], $ header ['mtime']);} Else {$ fp = @ fopen ($. $ header ['filename']. '.gz ', 'wb'); IF (! $ Fp) Return (-1); $ binary_data = pack ('va1a1va1a1 ', 0x8b1f, Chr ($ header ['compression']), Chr (0x00 ), time (), Chr (0x00), Chr (3); fwrite ($ fp, $ binary_data, 10 ); $ size = $ header ['compressed _ size']; While ($ size! = 0) {$ read_size = ($ size <1024? $ Size: 1024); $ buffer = fread ($ zip, $ read_size); $ binary_data = pack ('A '. $ read_size, $ buffer); @ fwrite ($ fp, $ binary_data, $ read_size); $ size-= $ read_size;} $ binary_data = pack ('vv ', $ header ['crc '], $ header ['SIZE']); fwrite ($ fp, $ binary_data, 8); fclose ($ fp ); $ gzp = @ gzopen ($. $ header ['filename']. '.gz ', 'rb') or die ("Cette archive est compress finished"); IF (! $ Gzp) Return (-2); $ fp = @ fopen ($ to. $ header ['filename'], 'wb'); IF (! $ Fp) Return (-1); $ size = $ header ['SIZE']; While ($ size! = 0) {$ read_size = ($ size <2048? $ Size: 2048); $ buffer = gzread ($ gzp, $ read_size); $ binary_data = pack ('A '. $ read_size, $ buffer); @ fwrite ($ fp, $ binary_data, $ read_size); $ size-= $ read_size;} fclose ($ fp); gzclose ($ gzp ); touch ($. $ header ['filename'], $ header ['mtime']); @ unlink ($. $ header ['filename']. '.gz ') ;}} Return true ;}}?>
Usage:
Function: File compression
Compression parameters: the file or folder to be compressed (the file can be an array), the zip file name and storage path after compression, compression type 1: folder 2: file, subsequent operations 1: download after compression; 2: stored on the server (under/@ Upload by default)
Example of a compressed Folder: Tozip ("./", "../". date ("d-H-I-s"). ". zip", 1, 2 );
Compressed file example:
$ Aaa = array ("../1.txt"," ../2.txt ");
Tozip ($ aaa, "../2.zip", 2 );
Example:
For example, there is a file structure like this:
Home
-- Upload
----1.jpg
----2.jpg
----3.jpg
-- Web
---- Html
------ Inc
-------- Zip. php (this is the code library above)
------ Download. php
Use the code in download. php to package and automatically download the upload Folder:
ZipDir ($ Path, $ ZipFile, $ Todo); Break; Case "2": $ zip-> ZipFile ($ Path, $ ZipFile, $ Todo); Break ;} IF ($ Todo = 1) {Die () ;}else {Return True ;}} Tozip (". /. /upload ",".. /.. /upload.zip ", 1, 1)?>
Original article address: PHP package to download the entire folder or multiple files. Thank you for sharing it with the original author.