php如何压缩一个文件夹里面所有的文件到zip文件里面?
如我的localhost下有个test文件夹,内部有很多文件和文件夹,如何把test里面所有东西打包成 test.zip?
------解决方案--------------------
- 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([email protected]_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), 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($to . $header['filename'] . '.gz', 'rb') or die("Cette archive est compress閑"); 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; }}