How does PHP compress all the files in a folder into a zip file?
How does PHP compress all the files in a folder into a zip file?
If there is a test folder under my localhost, there are many files and folders inside, how to package everything in Test into test.zip?
------Solution--------------------
PHP Code
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_len ']! = 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 ']) {$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 [' 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/vversion_extracted/vflag/vcompression/vmtime/vmdate/vcrc/vcompressed_size/ Vsize/vfilename_len/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 [' mtime '] = 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 = 277;} @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-Readfileheader ($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 ($to. $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 ($to. $header [' filename '], $header [' mtime ']; }else{$fp = @fopen ($to. $header [' filename ']. '. Gz ', ' WB '); IF (! $fp) Return (-1); $binary _data = Pack (' va1a1va1a1 ', 0x8b1f, Chr ($header [' compression ']), Chr (0x00), Time (), Chr (0x00), Ch R (3)); Fwrite ($fp, $binary _data, 10); $size = $header [' compressed_size ']; while ($size! = 0) {$read _size = ($size < 1024x768 $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 ($to. $header [' filename ']. '. gz', ' RB ') or die ("Cette archive est compress gluttony"); 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 ($to. $header [' filename '], $header [' mtime ']; @unlink ($to. $header [' filename ']. '. Gz '); }} Return true; }}